我將Google Firebase雲消息集成到了我的應用程序中。當我的應用程序處於前臺或後臺時,它可以接收來自Google服務器的消息推送,但是如果我的應用程序已經停用,它將無法收到來自Google服務器的消息推送。我想要的是,如果我的應用程序已經死了,它仍然可以從谷歌服務器收到消息。這裏是我的代碼:當我的應用程序使用Google Firebase雲消息時,我怎麼能收到推送消息
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FirebaseMsgServiceDemo";
@Override
public void onMessageReceived(RemoteMessage remotemsg) {
Log.d(TAG, "Demo Notification Body -> " + remotemsg.getNotification().getBody());
sendNotification(remotemsg.getNotification().getBody());
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Demo Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
String token = FirebaseInstanceId.getInstance().getToken();
Logger.e("TESTTTTT111111111", token);
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
}
}
<service
android:name="com.linkus.fcm.MyFirebaseInstanceIDService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name="com.linkus.fcm.MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
你是什麼意思「當應用程序死了」? –
最有可能你正在尋找這個:http://stackoverflow.com/questions/39504805/android-app-not-receiving-firebase-notification-when-app-is-stopped-from-multi-t –