She*_*eem 5 android reactjs react-native
我正在使用https://github.com/zo0r/react-native-push-notification进行本地推送通知。它还包括一个选项,可以在通知上设置一些按钮(“操作”),我可以按下这些按钮。
但是,当我收到通知时,为了响应该按钮(noti.action),应用程序应该打开(这是我可以解决它的唯一方法),只有这样我才能根据该操作运行我的逻辑。
因为我在使用react-native-push-notification时遇到了一些麻烦(它根本不起作用),所以我想自己实现这个。
这是我希望有的过程:
做这样的事情的正确方法是什么?我必须运行一个当用户按下贪睡按钮时触发的后台任务。我该如何使用 HeadlessJS 来实现这个目的?用 JavaScript 实现暂停逻辑非常容易,问题是如何触发它。
看起来您可以使用另一个库来实现此目的,即notifee库。
根据文档上的此页面,您可以像这样创建:
//[...]
notifee.displayNotification({
title: '08:00am Alarm',
body: 'The alarm you set for 08:00am requires attention!',
android: {
channelId: 'alarms',
actions: [
{
title: 'Snooze',
icon: 'https://my-cdn.com/icons/snooze.png',
pressAction: {
id: 'snooze'
},
},
{
title: 'not snooze',
icon: 'https://my-cdn.com/icons/notSnooze.png',
pressAction: {
id: 'snooze',
launchActivity: 'com.awesome.app.CustomActivity',
launchActivityFlags: []
},
},
],
},
});
//[...]
Run Code Online (Sandbox Code Playgroud)
在actions数组上,您可以提供您的活动,打开您的应用程序,并使用 launchActivityFlags 将一些参数发送到应用程序(如操作中所示not snooze)。
或者,您可以不提供任何内容,然后应用程序将无法打开,然后,您需要使用后台/前台事件的侦听器,如下所示:
import notifee, { EventType } from '@notifee/react-native';
//[...]
notifee.onForegroundEvent(({ type, detail }) => {
if (type === EventType.ACTION_PRESS && detail.pressAction.id) {
console.log('User pressed an action with the id: ', detail.pressAction.id);
//Use your code here to send your notification again after some time
}
//[...]
Run Code Online (Sandbox Code Playgroud)
您可以在此文档中查看有关通知操作的更多信息。
您可以在这个示例应用程序中看到notifee自己提供的一些代码。
如果您正在寻找闹钟应用程序,我找到了这个应用程序,但正如它看起来的那样,它不提供通知,所以它可能没有多大帮助。
[编辑]
要在用户忽略通知(他没有及时回复或立即滑动通知)时运行一段代码,您可以使用与 相同的概念onForegroundEvent,但进行一些更改,如下所示:
import notifee, { EventType } from '@notifee/react-native';
const timeout = false
//[...]
notifee.onForegroundEvent(({ type, detail }) => {
if(timeout) clearTimeout(timeout)
if (type === EventType.ACTION_PRESS && detail.pressAction.id){
console.log('User pressed an action with the id: ', detail.pressAction.id);
//Use your code here to send your notification again after some time
} else if (type === EventType.DISMISSED){
console.log('User dismissed the notification');
//Use your code here to send your notification again after some time, or maybe another action if the user dismissed the notification
} else if ([EventType.APP_BLOCKED, EventType.CHANNEL_BLOCKED, EventType.CHANNEL_GROUP_BLOCKED].includes(type)){
console.log("User cannot receive notification because the app is blocked")
//Run your code to tell the user that he needs to unblock notifications here.
} else if (type === EventType.DELIVERED){
console.log("Notification was delivered")
timeout = setTimeout(async () => {
console.log("Counting down the notification timeout")
//Run here your code to snooze the app if the user lost the notificaiton
}, 60000)
} else if (type === EventType.UNKNOWN){
console.log("Unknown error happened with the notification")
//Run your failsafe code here
}
//[...]
Run Code Online (Sandbox Code Playgroud)
根据文档:
DISMISSED:当用户取消通知时发送事件类型。这是通过用户从通知栏滑动通知或执行“清除所有”通知来触发的。当通知取消或超时时,不会发送此事件。
由于当通知取消或超时时不会发送此事件,并且我无法亲自尝试告诉您该情况的事件类型是什么,因此我使用 进行了超时倒计时EVENT.DELIVERED,如果没有任何反应,然后您可以运行代码来暂停闹钟。
DELIVERED:通知已传送到设备时发送的事件类型。对于触发器通知,此事件在触发器执行时发送,而不是在创建触发器通知时发送。
请务必注意,即使已发送通知,也可能不会向用户显示。例如,他们可能在设备/频道/应用程序上禁用了通知。
倒计时时间需要进行一些更改以适应您的用例(如果超时是动态的,则需要更多代码来在该函数上设置它)。
如果通知超时时有不同的事件,则可能不需要超时代码,但正如我在文档中阅读的那样,情况并非如此。
我还使用UNKNOWN事件类型作为故障保护,并使用APP_BLOCKED, CHANNEL_BLOCKED,CHANNEL_GROUP_BLOCKED来查看是否可以将通知传递给用户:
UNKNOWN:收到未知事件。此事件类型是一种故障保护功能,可捕获来自设备的任何未知事件。请报告复制问题,以便正确处理。
APP_BLOCKED:当用户更改整个应用程序的通知阻止状态或当用户打开应用程序设置时发送事件。
CHANNEL_BLOCKED:当用户更改应用程序中通道的通知阻止状态时发送事件类型。
CHANNEL_GROUP_BLOCKED :当用户更改应用程序中通道组的通知阻止状态时发送事件类型。
您可以在文档的此页面上阅读有关事件类型的更多信息。
| 归档时间: |
|
| 查看次数: |
326 次 |
| 最近记录: |