在响应本机推送通知中计划的推送通知操作onClick / onPress事件?

时间:2019-07-24 09:04:05

标签: react-native react-native-push-notification

我正在使用此程序包来实现本地推送通知:

https://github.com/zo0r/react-native-push-notification

我使用这样的操作按钮来在通知中显示按钮以及文本和标题:

PushNotification.localNotification({
...
actions: '["Yes", "No"]'
})

我想知道当用户单击这些操作并且应用程序可见时如何调用函数吗?

我已经在我的主屏幕上的PushNotification.configure方法中尝试过componentDidMount,但是控制台中什么也没出现:

  PushNotification.configure({
    // (required) Called when a remote or local notification is opened or received
    onNotification: function(notification) {
      console.log("NOTIFICATION:", notification);
      if (notification.userInteraction) {
        console.log("NOTIFICATION:");
      }

      // process the notification
    }
  });

4 个答案:

答案 0 :(得分:2)

我知道了。

在App.js中,您需要将popInitialNotification设置为true。像这样:

async componentDidMount() {
    PushNotification.configure({
      // (required) Called when a remote or local notification is opened or received
      onNotification: function(notification) {
        console.log("NOTIFICATION:", notification.action);
      },

      // IOS ONLY (optional): default: all - Permissions to register.
      permissions: {
        alert: true,
        badge: true,
        sound: true
      },

      // Should the initial notification be popped automatically
      // default: true
      popInitialNotification: true,

      /**
       * (optional) default: true
       * - Specified if permissions (ios) and token (android and ios) will requested or not,
       * - if not, you must call PushNotificationsHandler.requestPermissions() later
       */
      requestPermissions: true
    });
  }

notification.action将为您提供所单击按钮的标签。

答案 1 :(得分:1)

在您的按钮/应用活动事件中,您忘了打电话来安排通知并在发生通知时进行了实际设置,因此您需要

PushNotification.localNotificationSchedule(details: Object)

使用相同的id立即为立即安排时间,然后您的通知将立即出现。

查看所有用于安排here的选项

答案 2 :(得分:1)

import PushNotificationAndroid from 'react-native-push-notification'

(function() {
  // Register all the valid actions for notifications here and add the action handler for each action
  PushNotificationAndroid.registerNotificationActions(['Accept','Reject','Yes','No']);
  DeviceEventEmitter.addListener('notificationActionReceived', function(action){
    console.log ('Notification action received: ' + action);
    const info = JSON.parse(action.dataJSON);
    if (info.action == 'Accept') {
      // Do work pertaining to Accept action here
    } else if (info.action == 'Reject') {
      // Do work pertaining to Reject action here
    }
    // Add all the required actions handlers
  });
})();

答案 3 :(得分:0)

不要在组件,甚至是应用程序中使用 .configure()

如果这样做,通知处理程序将不会触发,因为它们未加载。相反,在应用的第一个文件中使用 .configure(),通常是 index.js。

文档中提到了这一点。

尝试按照他们的示例进行实施这将帮助您在项目中进行设置。

相关问题