我试图使用react-native-push-notification
包在React-Native中显示一些本地通知,但是当我触发通知时,它总是显示两次。这是我第一次尝试本机操作,因此我认为配置本身可能存在一些问题。
这是我为完成此工作而执行的代码:
已安装的软件包并已链接
- npm install-保存react-native-push-notification
- npx react-native链接react-native-push-notification
已更新的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"/>
react-native-app\android\app\src\main\res\values\colors.xml
并添加以下代码: <resources>
<color name="white">#FFF</color>
</resources>
react-native link
命令时已自动更新:
android / app / src / main / java / com / churchutility / MainApplication.java
import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;
android/build.gradle
,并在buildScript
ext
部分的下面添加了: buildscript {
ext {
+ googlePlayServicesVersion = "+"
+ firebaseVersion = "+"
----
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')
App.js
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
中有更好的方法或软件包来完成此操作。