为什么React本机推送通知总是显示两次?

时间:2020-04-19 11:20:45

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

我试图使用react-native-push-notification包在React-Native中显示一些本地通知,但是当我触发通知时,它总是显示两次。这是我第一次尝试本机操作,因此我认为配置本身可能存在一些问题。

这是我为完成此工作而执行的代码:

  1. 已安装的软件包并已链接

    • npm install-保存react-native-push-notification
    • npx react-native链接react-native-push-notification
  2. 已更新的android/app/src/main/AndroidManifest.xml文件:

    application标签上方添加了以下代码

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <permission
        android:name="${applicationId}.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />

在应用程序标记内添加了以下代码

      <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_name"
              android:value="YOUR NOTIFICATION CHANNEL NAME"/>
      <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_description"
                  android:value="YOUR NOTIFICATION CHANNEL DESCRIPTION"/>
      <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
                  android:resource="@color/white"/> 
  1. 创建文件react-native-app\android\app\src\main\res\values\colors.xml并添加以下代码:
      <resources>
          <color name="white">#FFF</color>
      </resources>
  1. 以下文件在运行react-native link命令时已自动更新:

    android / app / src / main / java / com / churchutility / MainApplication.java

      import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;
  1. 更新了android/build.gradle,并在buildScript ext部分的下面添加了:
 buildscript {
     ext {
+        googlePlayServicesVersion = "+"
+        firebaseVersion = "+"
 ----
  1. 文件android/settings.gradle已自动更新:
include ':react-native-push-notification'
project(':react-native-push-notification').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-push-notification/android')

  1. 在react native中,我创建了一个如下所示的类,并从App.js
  2. 创建了一个实例
import PushNotification from 'react-native-push-notification';

export default class ReminderService {
  constructor() {
    PushNotification.configure({
      onRegister: function(token) {
        global.alert('TOKEN:', token);
      },
      onNotification: function(notification) {
        global.alert('NOTIFICATION:', notification);
        // notification.finish(PushNotificationIOS.FetchResult.NoData);
      },
      permissions: {
        alert: true,
        badge: true,
        sound: true,
      },
      popInitialNotification: true,
      requestPermissions: true,
    });
    this.showNotification('Local notification', 'Not scheduled');
  }

  showNotification = (title, message) => {
    PushNotification.localNotification({
      title: title,
      message: message,
    });
  };
}

根据期望通知显示,但在模拟器和设备中始终显示两次。 请让我知道我在这里做错了什么,或者在react-native中有更好的方法或软件包来完成此操作。

0 个答案:

没有答案