我遇到了react-native-fcm
和react-native-push-notification
的一些问题,因此我决定尝试react-native-firebase
。
当应用在前景时,我可以接收服务器发送的以下有效载荷:
{
"to":"FCM_TOKEN",
"data": {
"icon": "http://host.com/img.jpg",
"title": "xyz title",
"body": "sadsaddsasadsda",
"click_action": "https://host.com/id/391",
"data": "some json.stringify custom data"
}
}
但是,当应用在后台时,FCM.onMessage()
根本没有收到任何通知。我可以使用react-native-fcm
或react-native-push-notification
在后台接收这些数据,所以我想知道这是否是react-native-firebase
的限制。
wixRoutes.js
import firebase from 'react-native-firebase';
import {Navigation} from 'react-native-navigation';
let FCM = firebase.messaging();
FCM.requestPermissions();
FCM.getToken().then(token=>{
console.log('has token',token);
});
FCM.onTokenRefresh((refreshedToken) => {
console.log(refreshedToken);
});
FCM.onMessage((message=>{
console.log('new message',message);
}));
FCM.getInitialNotification().then(notif => {
console.log('initial message',notif);
});
function startApp() {
Navigation.startSingleScreenApp(/..../);
}
startApp();
androidmanifest.xml
<!-- FCM -->
<service
android:name="io.invertase.firebase.messaging.MessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="io.invertase.firebase.messaging.InstanceIdService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<!-- If you would like to schedule local notifications then you also need to add the following:-->
<receiver android:name="io.invertase.firebase.messaging.RNFirebaseLocalMessagingPublisher"/>
<receiver android:enabled="true" android:exported="true" android:name="io.invertase.firebase.messaging.RNFirebaseSystemBootEventReceiver">
<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>
<activity android:launchMode="singleTop" /......./ />
原因是我的手机小米red Note 4X具有此选项为"不要保留活动"作为开发人员选项中的默认值,该选项在应用程序中后杀死了该应用无法收到任何通知。可拨打该选项,并且可以正常工作。