gpt4 book ai didi

react-native - 无法使用 react-native-firebase 在 Android 8 的前台接收通知

转载 作者:行者123 更新时间:2023-12-04 00:03:54 25 4
gpt4 key购买 nike

我仅在 Android 8 上无法在前台收到通知。也无法控制后台通知。 但在低于 8 的 Android 版本上,当前实现工作正常。

后续步骤:-

  1. 将 react-native-firebase 插件版本 5.0.0 安装到应用程序中。
  2. 在 firebase 控制台上创建项目并将 google_service.json 文件添加到 android/app 文件夹中。
  3. 在 AndroidManifest 中添加以下代码:-
     <service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="io.invertase.firebase.messaging.RNFirebaseInstanceIdService>
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService"/>

<receiver android:name="io.invertase.firebase.notifications.RNFirebaseNotificationReceiver"/>
<receiver android:enabled="true" android:exported="true" android:name="io.invertase.firebase.notifications.RNFirebaseNotificationsRebootReceiver">
<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="test_app"/>
  1. 在 App.js 页面添加以下函数并在 componentDidMountMethod() 中调用:-
performNotificationOperations(){
this.messageListener = firebase.messaging().onMessage((message: RemoteMessage) => {
console.log("Message",message);
alert("Notification Message Arrived");
if(this.state.isLogin){
const notification = new firebase.notifications.Notification()
.setNotificationId(message.messageId)
.setTitle(message.data.show_name)
.setBody(message.data.description)
.setData(message.data)
.android.setChannelId('test_app')
.android.setBigPicture(message.data.showImage)
.android.setPriority(firebase.notifications.Android.Priority.High);
firebase.notifications().displayNotification(notification).catch(err => alert("Error On Message"));
}
});
this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
console.log("Notification=>",notification);
alert("Notification Arrived");
if(this.state.isLogin){
notification.android.setChannelId('test_app')
notification.android.setBigPicture(notification._data.showImage);
notification.android.setPriority(firebase.notifications.Android.Priority.High)
firebase.notifications().displayNotification(notification).catch(err => alert("Error On Notification"));
}
});
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
console.log(notificationOpen,"Opened listener");
console.log(notificationOpen.notification._data.type,"notificationOpen");
firebase.notifications().removeDeliveredNotification(notificationOpen.notification._notificationId)
if(this.state.isLogin){
if(notificationOpen.notification._data.type==='show'){
Navigate.forward('myshowdetails', this._navigator, {show:notificationOpen.notification._data});
}else if(notificationOpen.notification._data.type==='episode'){
this.playEpisode(notificationOpen.notification._data.episodeToken);
Navigate.forward('myshowdetails', this._navigator, {show:notificationOpen.notification._data});
}
}
});
firebase.notifications().getInitialNotification()
.then((notificationOpen: NotificationOpen) => {
if (notificationOpen) {
alert('Initial Notification');
console.log(notificationOpen,"notificationOpen");
console.log(notificationOpen.notification._data.type,"notificationOpen");
firebase.notifications().removeDeliveredNotification(notificationOpen.notification._notificationId)
if(this.state.isLogin){
alert('IS LOGIN TRUE');
if(notificationOpen.notification._data.type==='show'){
Navigate.forward('showdetails', this._navigator, {show:notificationOpen.notification._data});
}else if(notificationOpen.notification._data.type==='episode'){
this.playEpisode(notificationOpen.notification._data.episodeToken);
Navigate.forward('showdetails', this._navigator, {show:notificationOpen.notification._data});
}
}
}
});
firebase.messaging().getToken().then(token => {
console.log("GCM Token====>>>>>>>>",token);
Global.GCM_TOKEN=token;
// alert(token);
if(Global.IS_USER_LOGIN){
Util.saveFCMToken(token);
}
});
}
  • 添加 bgMessage.js 文件来处理数据消息并使用 AppRegistery 注册 Headless JS 服务。
    // @flow
import firebase from 'react-native-firebase';
// Optional flow type
import type { RemoteMessage } from 'react-native-firebase';
import type { Notification,NotificationOpen} from 'react-native-firebase';

export default async (message: RemoteMessage) => {
// handle your message
// console.log("Message=>",message);
alert("Message Arrived");
const notification = new firebase.notifications.Notification()
.setNotificationId(message.messageId)
.setTitle(message.data.show_name)
.setBody(message.data.description)
.setData(message.data)
.android.setChannelId('podpitara_app')
.android.setBigPicture(message.data.showImage)
.android.setPriority(firebase.notifications.Android.Priority.High);
firebase.notifications().displayNotification(notification).catch(err => alert("Error in Background"));
return Promise.resolve();
}
  • headless JS 服务调用:-

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging);

  • 其他信息:-

Platform - Android

node-version - v8.6.0

react-native - 0.57.0

react-native-firebase - 0.5.0

  • 面临的问题

    1. 仅在 Android 8 的情况下无法在前台接收通知。

    2. 在背景/最小化状态的情况下,想要以大图显示通知,但无法控制我可以处理显示通知的位置。

    3. 在 Debug模式应用程序在通知托盘中正常显示图像的情况下,但在释放的情况下不显示图像。

请告诉我,我做错了什么。

最佳答案

让它工作。

这是因为从 Android 8 开始你需要创建一个 channel

// Build a channel
const channel = new firebase.notifications.Android.Channel('test-channel', 'Test Channel', firebase.notifications.Android.Importance.Max)
.setDescription('My apps test channel');

// Create the channel
firebase.notifications().android.createChannel(channel);

https://rnfirebase.io/docs/v4.2.x/notifications/android-channels

我只是在使用

android.setChannelId(message.data.channelId)

现在它会在应用处于前台时显示。

我的全功能是这样的

const newNotification = new firebase.notifications.Notification()
.android.setChannelId(message.data.channelId)
.setNotificationId(message.messageId)
.setTitle(message.data.title)
.setBody(message.data.body)
.setSound("default")
.setData(message.Data)
.android.setAutoCancel(true)
.android.setSmallIcon('ic_notification')
.android.setCategory(firebase.notifications.Android.Category.Alarm)

// Build a channel
const channelId = new firebase.notifications.Android.Channel(message.data.channelId, channelName, firebase.notifications.Android.Importance.Max);

// Create the channel
firebase.notifications().android.createChannel(channelId);
firebase.notifications().displayNotification(newNotification)

希望对你有帮助

关于react-native - 无法使用 react-native-firebase 在 Android 8 的前台接收通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52752871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com