vit*_*mal 0 android push-notification firebase react-native react-native-push-notification
我一直致力于使用firebase-admin
version^9.2.0
和react-native-push-notification
version添加自定义声音来推送react-native应用程序的通知^5.1.0
。
我没有升级到最新版本的原因react-native-push-notification
是,即使使用正确的通道配置,自定义声音似乎也不起作用。我们也在使用 expo,它在使用 7+ 版本时似乎会导致启动错误。
我有两个名为regular.mp3
和 的mp3 文件mass.mp3
。发送推送通知的 firebase 函数使用通用数据对象发送消息,还使用推送通知声音的平台特定字段:
admin.messaging().send({
data: {
title,
body,
lat: data.Latitude.toString(),
lng: data.Longitude.toString(),
notificationType: data.NotificationType.toString(),
},
notification:{title,body},
apns:{
payload:{
aps:{
alert:{
title,
body,
},
sound: data.NotificationType === 1 ? "mass.mp3" : "regular.mp3",
},
},
},
android: {
priority: "high",
data: {
sound: data.NotificationType === 1 ? "mass" : "regular",
},
notification: {
sound: data.NotificationType === 1 ? "mass" : "regular",
},
},
topic: topic,
})
Run Code Online (Sandbox Code Playgroud)
根据我的理解,data
下面的字段确实包含当应用程序被终止并收到通知时android
将添加到根级对象的有效负载。data
该插件的源代码似乎也使用该确切data
字段来设置通知声音(在 RNReceivedMessageHandler.java 中):
JSONObject data = getPushData(notificationData.get("data"));
if (data != null) {
if (!bundle.containsKey("message")) {
bundle.putString("message", data.optString("alert", null));
}
if (!bundle.containsKey("title")) {
bundle.putString("title", data.optString("title", null));
}
if (!bundle.containsKey("sound")) {
bundle.putString("soundName", data.optString("sound", null));
}
if (!bundle.containsKey("color")) {
bundle.putString("color", data.optString("color", null));
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止得到的:
以下是当前用于管理通知的代码:
在index.ts中:
PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android)
onRegister: function(token) {
console.log("TOKEN:", token);
},
// (required) Called when a remote is received or opened, or local notification is opened
onNotification: function(notification) {
console.log("NOTIFICATION:", notification);
// process the notification
// (required) Called when a remote is received or opened, or local notification is opened
//notification.finish(PushNotificationIOS.FetchResult.NoData);
},
// (optional) Called when Registered Action is pressed and invokeApp is false, if true onNotification will be called (Android)
onAction: function(notification) {
console.log("ACTION:", notification.action);
console.log("NOTIFICATION:", notification);
// process the action
},
// (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)
onRegistrationError: function(err) {
console.error(err.message, err);
},
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true,
},
popInitialNotification: true,
requestPermissions: true,
});
Run Code Online (Sandbox Code Playgroud)
在 App.js 中
CreateIncidentPushNotification=(remoteMessage)=>{
const {data} = remoteMessage;
PushNotification.localNotification({
title: data.title,
message: data.body,
playSound: true,
soundName: data.notificationType === "1" ? "mass" : "regular",
});
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否还有其他人知道会发生什么。即使应用程序被终止,通知仍然能够到达我的设备,这很棒。唯一缺少的部分是声音。
好吧,我终于成功了。我必须将以下内容添加到我的清单文件中并评论接收者:
<meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_name"
android:value="my-channel"/>
<meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_description"
android:value="my channel"/>
<!-- Change the value to true to enable pop-up for in foreground (remote-only, for local use ignoreInForeground) -->
<meta-data android:name="com.dieam.reactnativepushnotification.notification_foreground"
android:value="false"/>
<!-- Change the value to false if you don't want the creation of the default channel -->
<meta-data android:name="com.dieam.reactnativepushnotification.channel_create_default"
android:value="true "/>
<!-- Change the resource name to your App's accent color - or any other color you want -->
<meta-data android:name="com.dieam.reactnativepushnotification.notification_color"
android:resource="@color/white"/> <!-- or @android:color/{name} to use a standard color -->
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
<!-- <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver> -->
Run Code Online (Sandbox Code Playgroud)
然后,我必须使用通道才能使声音在前台、后台以及应用程序被终止时正常工作。如您所见,我在清单文件中创建了一个自定义通道,并激活了默认通道。我必须激活默认通道,因为我有两种需要不同声音的通知类型。使用单个通道是行不通的。
更新清单文件后,我修改了 firebase 函数(用于firebase-admin
执行以下操作:
admin.messaging().send({
data: {
title,
body,
lat: data.Latitude.toString(),
lng: data.Longitude.toString(),
notificationType: data.NotificationType.toString(),
},
notification:{title,body},
apns:{
payload:{
aps:{
alert:{
title,
body,
},
sound: data.NotificationType === 1 ? "mass.mp3" : "regular.mp3",
},
},
},
android: {
priority: "high",
data: {
sound: data.NotificationType === 1 ? "mass" : "regular",
channelId: data.NotificationType === 1 ? "my-channel" : "fcm_fallback_notification_channel",
},
notification: {
sound: data.NotificationType === 1 ? "mass" : "regular",
channelId: data.NotificationType === 1 ? "my-channel" : "fcm_fallback_notification_channel",
},
},
topic: topic,
})
.then((response) => {
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
Run Code Online (Sandbox Code Playgroud)
Firebase 现在知道我正在使用的两个通道。然后我转到应用程序代码并处理本地通知,如下所示:
PushNotification.localNotification({
title: data.title,
message: data.body,
playSound: true,
soundName: data.notificationType === "1" ? "mass" : "regular",
channelId: data.notificationType === "1" ? "my-channel" : "fcm_fallback_notification_channel"
});
Run Code Online (Sandbox Code Playgroud)
我还激活了包的onMessage
和处理程序:setBackgroundMessageHandler
react-native-push-notification
messaging().onMessage(this.sendMessage);
messaging().setBackgroundMessageHandler(this.sendMessage);
Run Code Online (Sandbox Code Playgroud)
哪里this.sendMessage
负责拨打localNotification
上面提到的电话。
顺便说一句,当应用程序处于后台时,我仍然收到重复的通知,因此这纯粹是对声音的修复。
更新:删除setBackgroundMessageHandler
已删除的重复项!!!!:) 和平!
归档时间: |
|
查看次数: |
8721 次 |
最近记录: |