
我在android和iPhone中使用react-native-fcm进行远程通知.
react-native-fcm
在AndroID前台我无法在通知栏中收到远程通知.
在后台模式下,我能够成功获得通知,但前景中的一些内容却没有.
AndroID Manifest.xml
<manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.nusape"> <application> <receiver androID:name="com.evollu.react.fcm.FIRLocalMessagingPublisher"/> <receiver androID:enabled="true" androID:exported="true" androID:name="com.evollu.react.fcm.FirsystemBootEventReceiver"> <intent-filter> <action androID:name="androID.intent.action.BOOT_COMPLETED"/> <action androID:name="androID.intent.action.QUICKBOOT_POWERON"/> <action androID:name="com.htc.intent.action.QUICKBOOT_POWERON"/> <category androID:name="androID.intent.category.DEFAulT" /> </intent-filter> </receiver> <Meta-data androID:name="com.Google.firebase.messaging.default_notification_icon" androID:resource="@mipmap/ic_launcher"/> <Meta-data androID:name="com.Google.firebase.messaging.default_notification_channel_ID" androID:value="my_default_channel"/> <service androID:name="com.evollu.react.fcm.MessagingService" androID:enabled="true" androID:exported="true"> <intent-filter> <action androID:name="com.Google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <service androID:name="com.evollu.react.fcm.InstanceIDService" androID:exported="false"> <intent-filter> <action androID:name="com.Google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service> <activity androID:launchMode="singletop" androID:configChanges="keyboard|keyboardHIDden|orIEntation|screenSize" androID:windowsoftinputMode="adjustResize"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action androID:name="fcm.ACTION.HELLO" /> <category androID:name="androID.intent.category.DEFAulT" /> </intent-filter> </activity> <activity androID:name="com.facebook.react.devsupport.DevSettingsActivity" /> </application></manifest>App.Js
async componentDIDMount() {// create NotificationChannel for future use! FCM.createNotificationChannel({ ID: 'my_default_channel', name: 'Default', description: 'used for example', priority: 'high' }); // initially user get InitialNotification frim the app if any pending FCM.getinitialNotification().then(notif => { console.log("getinitialNotification Notification : => ", notif); // if notif.targetScreen is details screen then it will redirect to details screen directly! if (notif && notif.targetScreen === "detail") { setTimeout(() => { this.props.navigation.navigate("Detail"); }, 500); } }); // added notification Listener for getting any notification called below function then this.notificationListener = FCM.on(Fcmevent.Notification, async (notif) => { console.log("Fcmevent.Notification Notification : => ", notif); if (Platform.OS === 'ios' && notif._notificationType === NotificationType.WillPresent && !notif.local_notification) { notif.finish(WillPresentNotificationResult.All); return; } // if user tap to notification bar then open app then below condition will follow up and redirect to details screen! if (notif.opened_from_tray) { if (notif.targetScreen === 'detail') { setTimeout(() => { navigation.navigate('Detail') }, 500) } setTimeout(() => { alert(`User tapped notification\n${JsON.stringify(notif)}`) }, 500) } // check whether app is in background or foreground for generate notification if (AppState.currentState !== 'background'){ this.showlocalnotification(notif); }); // getting user permission for sending notification or not ? try { let result = await FCM.requestPermissions({ badge: true, sound: true, alert: true }); console.log("Notification requestPermissions : => ", result) } catch (e) { console.error(e); } // Generating token for particular user wise send notification FCM.getFCMToken().then(token => { FCM.subscribetotopic("channelTotopic"); console.log("Notification token : => ", token); this.setState({ token: token || "" }); }); // Get APNSTOKEN for only ios if (Platform.OS === "ios") { FCM.getAPNSToken().then(token => { console.log("APNS TOKEN (getFCMToken)", token); }); } } // show notification when app is in foreground and getting any new notification showlocalnotification = (notif) => { FCM.presentlocalnotification({ channel: 'my_default_channel', ID: new Date().valueOf().toString(), Title: notif.fcm.Title, body: notif.fcm.body, priority: "high", badge: 1, number: 1, ticker: "My Notification Ticker", auto_cancel: true, big_text: "Show when notification is expanded", sub_text: "This is a subText", wake_screen: true, group: "group", icon: "ic_launcher", ongoing: true, my_custom_data: "my_custom_fIEld_value", lights: true, show_in_foreground: true }); };我在过去的两个月里遇到了这个问题并没有得到很好的解决方案,因为我做了很多新的尝试来解决问题,但最终没有取得任何成功.
解决方法:
根据官方Github的说法
react-native-fcm,
这个库是折旧的.
你可以使用
react-native-firebase
用于生成通知.
我能够在大约2个小时内获得通知工作.
如果你想要我可以分享它的代码.
祝好运.
更新 – 抱歉,由于我的办公室帐户,我之前无法回答.
这是我显示androID前台通知的代码.
firebase.messaging() .subscribetotopic(this.state.user.user_name) .then(response => console.log('response from FCM topIC' + response)) .catch(error => console.log('error from FCM topIC'+ error)); this.notificationListener = firebase.notifications().onNotification(notification => { let notificationMessage = notification._androID._notification._data.action; let recordID = notification._androID._notification._data.recordID; let { Title, body } = notification; // console.log('ttttt', notification) // notification.androID.setautoCancel(false) console.log(Title, body, notificationMessage, recordID); this.getinspectionUserLogs(this.state.user); const channelID = new firebase.notifications.AndroID.Channel( 'Default', 'Default', firebase.notifications.AndroID.importance.High ); firebase.notifications().androID.createChannel(channelID); let notification_to_be_displayed = new firebase.notifications.Notification({ data: notification._androID._notification._data, sound: 'default', show_in_foreground: true, Title: notification.Title, body: notification.body, }); if (Platform.OS == 'androID') { notification_to_be_displayed.androID .setPriority(firebase.notifications.AndroID.Priority.High) .androID.setChannelID('Default') .androID.setVibrate(1000); } console.log('FOREGROUND NOTIFICATION ListENER: \n', notification_to_be_displayed); firebase.notifications().displayNotification(notification_to_be_displayed); }); 总结 以上是内存溢出为你收集整理的推送通知未在Android前景中显示全部内容,希望文章能够帮你解决推送通知未在Android前景中显示所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)