后台代理提醒开发指导


后台代理提醒开发指导

场景介绍

后台代理提醒主要提供后台提醒发布接口,开发者在应用开发时,可以调用这些接口去创建定时提醒,包括倒计时、日历、闹钟三种提醒类型。使用后台代理提醒能力后,应用可以被冻结或退出,计时和弹出提醒的功能将被后台系统服务代理。

接口说明

reminderAgentManager:封装了发布、取消提醒类通知的方法。

具体后台提醒相关功能接口请见后台代理提醒

表1 reminderAgentManager主要接口

接口名 描述
publishReminder(reminderReq: ReminderRequest, callback: AsyncCallback<number>): void
publishReminder(reminderReq: ReminderRequest): Promise<number>
发布一个定时提醒类通知。
单个应用有效的提醒个数最多支持30个(不包括已经超时,即后续不会再提醒的提醒实例)
整个系统有效的提醒个数最多支持2000个(不包括已经超时,即后续不会再提醒的提醒实例)
cancelReminder(reminderId: number, callback: AsyncCallback<void>): void
cancelReminder(reminderId: number): Promise<void>
取消一个指定的提醒类通知。(reminderId从publishReminder的返回值获取)
getValidReminders(callback: AsyncCallback<Array<ReminderRequest>>): void
getValidReminders(): Promise<Array<ReminderRequest>>
获取当前应用设置的所有有效的提醒。
cancelAllReminders(callback: AsyncCallback<void>): void
cancelAllReminders(): Promise<void>
取消当前应用设置的所有提醒
addNotificationSlot(slot: NotificationSlot, callback: AsyncCallback<void>): void
addNotificationSlot(slot: NotificationSlot): Promise<void>
注册一个提醒类需要使用的NotificationSlot
removeNotificationSlot(slotType: notification.SlotType, callback: AsyncCallback<void>): void
removeNotificationSlot(slotType: notification.SlotType): Promise<void>
删除指定类型的NotificationSlot

开发步骤

说明:

  1. 应用需要配置权限:ohos.permission.PUBLISH_AGENT_REMINDER。

  2. 应用需要申请通知弹窗:Notification.requestEnableNotification。 只有用户授权后,提醒代理的功能才能使用。

1、定义目标提醒代理。

2、发布相应的提醒代理。

import reminderAgentManager from '@ohos.reminderAgentManager';
import notification from '@ohos.notification';

// 倒计时实例定义:
let timer : reminderAgentManager.ReminderRequestTimer = {
    reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER,
    triggerTimeInSeconds: 10,
    actionButton: [
        {
            title: "close",
            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
        }
    ],
    wantAgent: {
        pkgName: "com.example.device",
        abilityName: "com.example.device.MainAbility"
    },
    maxScreenWantAgent: {
        pkgName: "com.example.device",
        abilityName: "com.example.device.MainAbility"
    },
    title: "this is title",
    content: "this is content",
    expiredContent: "this reminder has expired",
    notificationId: 100,
    slotType: notification.SlotType.SOCIAL_COMMUNICATION
}

// 日历实例定义:
let calendar : reminderAgentManager.ReminderRequestCalendar = {
    reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_CALENDAR,
    dateTime: {
        year: 2050,
        month: 7,
        day: 30,
        hour: 11,
        minute: 14,
        second: 30
    },
    repeatMonths: [1],
    repeatDays: [1],
    actionButton: [
        {
            title: "close",
            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
        },
        {
            title: "snooze",
            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
        },
    ],
    wantAgent: {
        pkgName: "com.example.device",
        abilityName: "com.example.device.MainAbility"
    },
    maxScreenWantAgent: {
        pkgName: "com.example.device",
        abilityName: "com.example.device.MainAbility"
    },
    ringDuration: 5,
    snoozeTimes: 2,
    timeInterval: 5,
    title: "this is title",
    content: "this is content",
    expiredContent: "this reminder has expired",
    snoozeContent: "remind later",
    notificationId: 100,
    slotType: notification.SlotType.SOCIAL_COMMUNICATION
}

// 闹钟实例定义:
let alarm : reminderAgentManager.ReminderRequestAlarm = {
    reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_ALARM,
    hour: 11,
    minute: 14,
    daysOfWeek: [0],
    actionButton: [
        {
            title: "close",
            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
        },
        {
            title: "snooze",
            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
        },
    ],
    wantAgent: {
        pkgName: "com.example.device",
        abilityName: "com.example.device.MainAbility"
    },
    maxScreenWantAgent: {
        pkgName: "com.example.device",
        abilityName: "com.example.device.MainAbility"
    },
    ringDuration: 5,
    snoozeTimes: 2,
    timeInterval: 5,
    title: "this is title",
    content: "this is content",
    expiredContent: "this reminder has expired",
    snoozeContent: "remind later",
    notificationId: 100,
    slotType: notification.SlotType.SOCIAL_COMMUNICATION
}

@Entry
@Component
struct Index {
  @State message: string = 'test'

  publishReminder() {
    try {
        reminderAgentManager.publishReminder(timer).then(res => {
            console.log("publishReminder promise reminderId:" + res);
        }).catch(err => {
            console.log("publishReminder err code:" + err.code + " message:" + err.message);
        })
    } catch (error) {
        console.log("publishReminder code:" + error.code + " message:" + error.message);
    };
  }

  build() {
    Row() {
      Column() {
        Text("Index")
          .fontSize(50)
          .fontWeight(FontWeight.Bold)

        Button() { Text('倒计时提醒代理').fontSize(25).fontWeight(FontWeight.Bold) }.type(ButtonType.Capsule)
          .margin({ top: 10 }).backgroundColor('#0D9FFB').width(250).height(40)
          .onClick(() => {
          // 示例通过按钮控制提醒代理的发布
          this.publishReminder();
        })

      }
      .width('100%')
    }
    .height('100%')
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

相关实例

基于后台代理提醒开发,有以下相关实例可供参考: