问题描述::
我正在研究React Native Application,并使用React Native Firebase消息传递服务进行推送通知。我在iOS平台上遇到了问题。我正在为单个事件进行双重通知弹出窗口。
我遵循的步骤生成案例::
-
在安装应用程序后,如果我登录并通过FCM发送通知,我刚刚收到了一个弹出窗口。此后,我登录并再次登录,现在这次我获得了单次通知的双弹出窗口。在这种情况下,我不会从背景中清除应用程序。
-
如果每次注销后,我都从背景清理一个应用程序,我刚刚收到了一个单个事件的单个弹出窗口。
-
当我从应用程序中登录并从FCM上发送通知时,我会在应用程序初始化屏幕(登录屏幕)上获得双弹出。
我将在用户登录并保存该令牌内的本地存储中生成一个新设备令牌,我们正在清除注销中的本地存储数据。
代码::
async mountDashboard() {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
const fcmToken = await firebase.messaging().getToken();
await AsyncStorage.setItem(LocalStorageKeys.DEVICE_TOKEN, fcmToken);
if (fcmToken) {
//--- here we are saving our token and sendind data to API
}
}
// in Foreground
this.notificationListener = firebase.notifications().onNotification((notification) => {
new RegisterLocalNotification(notification);
});
// App in Foreground and background
let notificationHandler = new NotificationsHandler();
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
notificationHandler.handleNotification(notificationOpen.notification);
});
// app close notification handler
AppCloseNotificationHandler.getInstance().handleNotification();
}
componentDidMount() {
this.mountDashboard();
}
环境::
binaries:
- 节点:10.15.0-/usr/local/opt/node@10/bin/node
- Yarn:1.10.1-/usr/local/bin/yarn
- npm:6.4.1-/usr/local/opt/node@10/bin/npm
- 守望者:4.9.0-/usr/local/bin/watchman
- 列表项目
NPM软件包:
- react: ^16.8.4 => 16.8.4
- 反应: ^0.58.6 => 0.58.6
NPM全局软件包:
- react-native-cli:2.0.1
- React-Native-Firebase:5.2.3
当组件卸下时,您必须退订侦听器。如果您不这样做,则订阅了两个听众。
componentWillUnmount() {
this.notificationListener(); // it's unsubscribing your listener
}
确保通过后端发送此有效载荷,我正在使用Firebase Admin SDK。这将通过OS禁用通知并触发本地通知。我正在使用此软件包进行本地通知
async function sendPushNotification(message) {
try {
await admin.messaging().sendToDevice(
['dCk27uEySymSdP_vA1C_QI:APA91bEZNyipZDjTLq0jrGnD0qcpKH2y3oTYg3GMgT0pSENNlJEiymOYXvxvnqTFtQaidDLt5KUgp4oDZsfLsQvfiVkL7m1bpMjekHsm-7x1ZDju4TYUMUZeUgYb0CyPwMhdr9RyzA1v'],
{
data: {
owner: 'a',
user: 'a',
},
},
{
// Required for background/quit data-only messages on iOS
contentAvailable: true,
// Required for background/quit data-only messages on Android
priority: 'high',
},
);
console.log('A');
}catch(e) {
console.log('Gagal mengirim pesan');
}
}
这是我的代码在app.js中,聆听通知
rnfirebase.onmessage(rnfirebaseonMessageHandler);rnfirebase.setBackgroundMessageHandler(rnfirebaseonMessageHandler);
和处理程序内部,我使用此
PushNotification.createChannel(
{
channelId: CHANNEL_ID,
channelName: CHANNEL_NAME,
channelDescription: CHANNEL_DESCRIPTION,
soundName: CHANNEL_SOUND,
importance: CHANNEL_IMPORTANCE,
vibrate: CHANNEL_VIBRATE,
},
(created) => console.log(`createChannel returned '${created}'`), // (optional) callback returns whether the channel was created, false means it already existed.
);
PushNotification.localNotification({
channelId: CHANNEL_ID,
// @todo get ticker value from data payload
ticker: 'CreateOrderLandingScreen',
showWhen: true,
autoCancel: true,
largeIcon: CHANNEL_LARGE_ICON,
smallIcon: CHANNEL_SMALL_ICON,
bigText: 'aaaaa',
subText: CHANNEL_SUB_TEXT,
color: Colors.RED,
vibrate: true,
vibration: 300,
priority: 'high',
visibility: 'private',
invokeApp: true,
alertAction: 'view',
id: 0,
title:'aa',
message: 'aaaa',
userInfo: {},
playSound: false,
soundName: 'default',
});