android react native push notification sooze(react-native-push-notification)

ou6hu8tu  于 2023-11-15  发布在  Android
关注(0)|答案(1)|浏览(125)

我用https://github.com/zo0r/react-native-push-notification来做本地推送通知,它还提供了一个选项,可以在通知上设置一些按钮(“操作”),我可以按下它们。
然而,当我收到一个通知时,为了响应那个按钮(noti. action),应用程序应该打开(我唯一能解决的方法),只有这样我才能根据动作运行我的逻辑。
因为我在使用react-native-push-notification时遇到了一些麻烦(它根本不起作用),所以我想自己实现它。
这是我希望的过程:
1.推送通知正在显示
1.用户点击贪睡按钮(与闹钟相同)
1.应用程序仍在后台
1.通知将在一段时间后再次推送。
什么是正确的方法来做这样的事情?我必须运行一个后台任务,当用户按下贪睡按钮时会触发。我如何使用HeadlessJS来实现这一点?用JavaScript实现贪睡逻辑非常容易,问题是如何触发它。

p5cysglq

p5cysglq1#

看起来你可以使用另一个库,notifee库。
根据文档上的this页面,您可以像这样创建:

//[...]

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: []
        },
      },
    ],
  },
});

//[...]

字符串

说明

actions数组中,您可以提供Activity以打开应用,并使用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
  }
//[...]


您可以在this文档中查看有关通知操作的更多信息。
你可以在notifee自己提供的this示例应用程序上看到一些代码。
如果,你正在寻找一个闹钟应用程序,我发现this应用程序,但它看起来像,它不提供通知,所以它可能没有那么大的帮助.
[编辑]
如果用户拒绝了通知(他没有及时回复,或者马上刷掉了通知),要运行一段代码,可以使用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
  }
//[...]

说明

根据文档:
解散:当用户解除通知时发送事件类型。这是通过用户从通知阴影中滑动通知或执行“清除所有”通知来触发的。当通知取消或超时时不会发送此事件。
由于在通知被取消或超时时不会发送此事件,并且我无法亲自尝试并告诉您这种情况下的事件类型,因此我使用EVENT.DELIVERED对通知超时进行了超时倒计时,如果没有发生任何情况,则可以运行代码以使警报暂停。
交付日期:通知已传递到设备时发送的事件类型。对于触发器通知,此事件在触发器执行时发送,而不是在创建触发器通知时发送。
需要注意的是,即使通知已经发送,也可能不会显示给用户。例如,他们可能在设备/频道/应用程序上禁用了通知。
倒计时时间将需要一些更改以适应您的用例(如果超时是动态的,则需要更多的代码来设置该函数)。
如果通知超时时有不同的事件,则可能不需要超时代码,但正如我在文档中所读到的那样,情况并非如此。
我还使用了UNKNOWN事件类型作为故障保护,并使用了APP_BLOCKEDCHANNEL_BLOCKEDCHANNEL_GROUP_BLOCKED来查看通知是否可以传递给用户:
未知:收到未知事件。此事件类型是一种故障保护,可捕获来自设备的任何未知事件。请报告复制问题,以便正确处理。
APP_BLOCKED:当用户更改整个应用程序的通知阻止状态或当用户打开应用程序设置时,将发送。
CHANNEL_BLOCKED:当用户更改应用程序中通道的通知阻止状态时,将发送事件类型。
CHANNEL_GROUP_BLOCKED:当用户更改应用程序中通道组的通知阻止状态时发送事件类型。
您可以在此页面上的文档中阅读有关事件类型的更多信息。

相关问题