react-native - 在本机反应中无法从 fcm 获得推送通知
问题描述
我从 firebase 收到推送通知,但是当我使用 react-native-firebase 库在 android 上使用“ https://fcm.googleapis.com/fcm/send ”发送它时,我没有收到任何关于 android 的通知. 但是,我可以使用“onMessage”方法在控制台上显示消息。但是我如何在通知栏中获得通知。我的消息是纯数据的,因此我还创建了 bgMessaging.js 来处理后台消息,在这里我也可以在控制台上显示消息,但不能在通知上显示。
如何解决此问题并在通知栏上显示带有纯数据消息的消息。
下面是我的代码
bgMessaging.js
import firebase from 'react-native-firebase';
// Optional flow type
import type { RemoteMessage } from 'react-native-firebase';
export default async (message: RemoteMessage) => {
// handle your message
console.log("message")
console.log(message)
// senName = message.data.senderName;
// senUid = message.data.senderUid;
// const notification = new
// firebase.notifications.Notification()
// .setNotificationId('notificationId')
// .setTitle(message.data.title)
// .setBody(message.data.body)
// .android.setChannelId('channel_id_foreground')
// .android.setSmallIcon('ic_launcher');
// firebase.notifications().displayNotification(notification);
return Promise.resolve();
}
index.js(在末尾添加以下行)
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging); // <-- Add this line
应用程序.js
componentDidMount() {
this.messageListener = firebase.messaging().onMessage((message: RemoteMessage) => {
//process data message
console.log(message);
});
}
AndroidManifest.xml
<service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" />
<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=".MyTaskService" />
解决方案
我遇到了完全相同的问题,我收到了纯数据消息,但无法显示通知。
我发现为了显示 8+ Android 版本的通知,您需要先创建一个 Android 频道,代码:
// Create Channel first.
const channel = new firebase.notifications.Android.Channel(
"general-channel",
"General Notifications",
firebase.notifications.Android.Importance.Default
).setDescription("General Notifications");
firebase.notifications().android.createChannel(channel);
// Build your notification
const notification = new firebase.notifications.Notification()
.setTitle(...)
.setBody(...)
.setNotificationId("notification-action")
.setSound("default")
.setData(message.data)
.android.setChannelId("general-channel")
.android.setPriority(firebase.notifications.Android.Priority.Max);
// Display the notification (with debug)
firebase
.notifications()
.displayNotification(notification)
.catch(err => console.error(err));
推荐阅读
- jquery-ui - 如何使用 Jquery ui 在 DataTable 中放置模态框
- python - 如何在python中使用函数作为参数
- python - 如何跨多个工作表对相同的列进行排序?
- python - 在 read_pickle 之后追加
- ruby - Ruby / Redmine:从问题主题和适当的自定义字段编译哈希
- python - AttributeError:“历史”对象没有属性“predict_classes”
- android - org.jsoup.HttpStatusException:获取 URL 的 HTTP 错误。状态=403
- python - 使用 pygame.KEYDOWN 拾取方块
- pyspark - 计算 PySpark 中数组的滚动总和并保存为 dict?
- c# - 异步等待与等待 task.run