【发布时间】:2017-02-14 06:12:38
【问题描述】:
我尝试将 Firebase 用于 Android 推送通知。但我遇到了非常奇怪的问题。当我在前台发送推送时一切正常。当我在后台发送推送时一切正常(我只发送数据),直到我回到前台然后回到后台。
当我的应用第二次进入后台时,不会调用我的 FirebaseMessagingService。另外,请注意我使用的是 Android 模拟器。代码:
public class MyAndroidFirebaseMsgService extends FirebaseMessagingService {
private static final String TAG = "MyAndroidFCMService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
}
}
//...
public class MyAndroidFirebaseInstanceIdService extends FirebaseInstanceIdService {
private static final String TAG = "MyAndroidFCMIIDService";
@Override
public void onTokenRefresh() {
//Get hold of the registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Log the token
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
private void sendRegistrationToServer(String token) {
//Implement this method if you want to store the token on your server
}
}
AndroidManifest.xml:
<service android:name=".MyAndroidFirebaseMsgService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".MyAndroidFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
服务器:
curl -H "Content-type: application/json" -H "Authorization:key=<MYKEY>" -X POST -d '{"to": "<MYTOKEN>","data": {"hello": "This is a Firebase Cloud Messaging Device Group Message!"}}' https://fcm.googleapis.com/fcm/send
我该如何解决?
【问题讨论】:
标签: android firebase android-service android-notifications firebase-cloud-messaging