我在我的应用中使用react-native-firebase进行云消息传递和通知。在使用应用程序时可以获取所有正文和数据,但是在关闭应用程序或在后台时无法获取任何内容。
我尝试了Headless JS,但是那也不起作用
当我单击通知时,当它打开时,应用会显示此{"google.priority":"high"}
先想一下......
这是我的android mainfest
<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:allowBackup="false"
android:theme="@style/AppTheme"
android:largeHeap="true">
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_ic_notification" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
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>
</activity>
<!---firebase -->
<service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<!-- <meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/> -->
<service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" />
<!---firebase end-->
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>
这是我的componentdidmount()函数
async componentDidMount() {
this.getValueLocally();
requestCameraLOCATION()
const notificationOpen: NotificationOpen = await firebase.notifications().getInitialNotification();
if (notificationOpen) {
const action = notificationOpen.action;
const notification: Notification = notificationOpen.notification;
var seen = [];
alert(JSON.stringify(notification.data, function(key, val) {
if (val != null && typeof val == "object") {
if (seen.indexOf(val) >= 0) {
return;
}
seen.push(val);
}
return val;
}));
}
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);
this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => {
// Process your notification as required
// ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification.
});
this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
// Process your notification as required
notification
.android.setChannelId('test-channel')
.android.setSmallIcon('ic_launcher');
firebase.notifications()
.displayNotification(notification);
});
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
// Get the action triggered by the notification being opened
const action = notificationOpen.action;
// Get information about the notification that was opened
const notification: Notification = notificationOpen.notification;
var seen = [];
alert(JSON.stringify(notification.data, function(key, val) {
if (val != null && typeof val == "object") {
if (seen.indexOf(val) >= 0) {
return;
}
seen.push(val);
}
return val;
}));
firebase.notifications().removeDeliveredNotification(notification.notificationId);
});
}
async checkPermission() {
firebase.messaging().hasPermission()
.then(enabled => {
if (enabled) {
this.getToken();
} else {
this.requestPermission();
}
});}
async getToken() {
let fcmToken = await AsyncStorage.getItem('fcmToken');
if (!fcmToken) {
fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
// user has a device token
await AsyncStorage.setItem('fcmToken', fcmToken);
}
}
}
async requestPermission() {
firebase.messaging().requestPermission()
.then(() => {
this.getToken();
})
.catch(error => {
console.warn(error);
});
}
这是我的bgMessaging.js
import firebase from 'react-native-firebase';
import type { RemoteMessage } from 'react-native-firebase';
import type { Notification,NotificationOpen} from 'react-native-firebase';
export default async (message: RemoteMessage) => {
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)
return Promise.resolve();
}
答案 0 :(得分:0)
有两种类型的消息:
library(dplyr)
df %>% group_by(commune) %>%
summarise(house_pros=n_distinct(container), #See dplyr::n_distinct
c_found=n(), # Size of each group
aegypti_Pos_container=sum(aegypti!=0)) %>% #Num of aegypti !=0
bind_rows(.,tibble(commune='Total',house_pros=sum(.$house_pros),c_found=sum(.$c_found),
aegypti_Pos_container=sum(.$aegypti_Pos_container))) %>%
mutate(aegypti_In=(aegypti_Pos_container/house_pros)*100)
# A tibble: 5 x 5
commune house_pros c_found aegypti_Pos_container aegypti_In
<chr> <int> <int> <int> <dbl>
1 yde2 1 1 1 100
2 yde3 2 2 1 50
3 yde4 2 2 2 100
4 yde7 4 5 4 100
5 Total 9 10 8 88.9
消息,将在后台由FCM处理(因此您的代码将无权访问该通知),而在前景中将调用notification + data
,
onNotification
消息在后台/关闭时将调用headlessjs,而在前景时将调用data-only
。
注意:如果删除onMessage
和title
,则您的邮件将被分类为第二个。同样,数据在第一类中是可选的。
回顾:
前台应用程序:已触发body
后台应用/关闭的应用:后台处理程序(HeadlessJS)
前台应用程序:已触发onMessage
后台运行的应用/关闭的应用:如果点击通知,则onNotification
会触发
有关更多信息和iOS版本,请阅读官方文档for data-only messages和notification+data messages
答案 1 :(得分:0)
似乎RN firebase在Android上不支持此功能。
请参考以下内容:
不幸的是,在Android上,无法访问已打开的远程通知的标题和正文。如果需要,您可以使用远程通知的数据部分来提供此信息。