我遇到了一些问题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
or接收这些数据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" /......./ />