【问题标题】:how to detect Onpress in Push notification React Native如何在推送通知 React Native 中检测 Onpress
【发布时间】:2021-05-06 12:47:22
【问题描述】:

我在我的项目中使用react-native-push-notification 包。当用户点击通知时。如何使用 OnNotification 函数在 android 和 ios 中检测它?这是代码

import PushNotification from "react-native-push-notification";
import PushNotificationIOS from "@react-native-community/push-notification-ios";

PushNotification.configure({
    onRegister: function (token) {
      console.log("TOKEN:", token);
    },

    onNotification: function (notification) {
      console.log("[onNotification] :", notification);
 
      notification.finish(PushNotificationIOS.FetchResult.NoData);
    },

    onAction: function (notification) {
      console.log("[onAction]", notification.action);
      console.log("[onAction]:", notification);
    },

    onRegistrationError: function (err) {
      console.error(err.message, err);
    },

    permissions: {
      alert: true,
      badge: true,
      sound: true,
    },

    popInitialNotification: true,

    requestPermissions: true,
  });

【问题讨论】:

    标签: react-native react-native-android react-native-ios react-native-firebase react-native-push-notification


    【解决方案1】:

    您可以使用notification 对象参数的userInteraction 属性传递给onNotification 回调,如他们的documentation 中所述。

    这是一个例子

    PushNotification.configure({
      onNotification: function (notification) {
        if (notification.userInteraction) {
          // Handle notification click
        }
    
        notification.finish(PushNotificationIOS.FetchResult.NoData);
      },
    });
    

    【讨论】:

    • 它在 Ios 中运行良好,但在 Android 中,如果我按下通知,则没有任何反应。即 onNotification: 函数不是在 android 中呈现 agin,而是在 ios 中,当我打开通知时 onNotification 返回一些要处理的对象
    • 我在 splashactivity.java 中做了一些更改来触发 Android 中的 onNotification。 github.com/zo0r/react-native-push-notification/issues/1976
    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 2015-06-23
    • 1970-01-01
    相关资源
    最近更新 更多