2017-07-22 41 views
0

我想在我的應用中使用Firebase雲消息傳遞來獲取通知。如何在應用關閉時使用Firebase雲消息傳遞通知

我收到通知時,應用程序是在前臺或後臺,但如果應用程序被殺死,那麼它不起作用。我知道,如果沒有在關閉應用程序後運行服務之類的服務,這是不可能的,但我不知道如何實現它。

請指導我。提前致謝。

我正在使用這個類來獲取通知。

public class FcmMessagingService extends FirebaseMessagingService { 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     String title= remoteMessage.getNotification().getTitle(); 
     String message= remoteMessage.getNotification().getBody(); 
     Intent intent= new Intent(this,MainActivity.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent= PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT); 
     NotificationCompat.Builder notificationBuilder= new NotificationCompat.Builder(this); 
     notificationBuilder.setContentTitle(title); 
     notificationBuilder.setContentText(message); 
     notificationBuilder.setAutoCancel(true); 
     notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); 
     notificationBuilder.setContentIntent(pendingIntent); 
     NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0,notificationBuilder.build()); 
    } 
} 
+0

[Android應用程序不接受火力地堡通知的可能的複製,當應用程序從多停止任務托盤](https:// stackove rflow.com/questions/39504805/android-app-not-receiving-firebase-notification-when-app-is-stopped-from-multi-t) –

回答

0

如果您是從火力控制檯消息發送一些數據有效載荷,你不會得到你的自定義通知時,應用程序在後臺或滅活狀態,你會得到,如果你想獲得通知時,應用程序是默認火力通知在死亡或後臺狀態下從你的服務器在這裏發送POST請求https://fcm.googleapis.com/fcm/send這個網址結賬完整指南https://firebase.google.com/docs/cloud-messaging/send-message和您的messageReceive檢查數據

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    // Check if message contains a data payload. 
    if (remoteMessage.getData().size() > 0) { 
     if (remoteMessage.getData() != null) { 
      Map<String, String> dataMap = remoteMessage.getData(); 
      String title = dataMap.get("title"); 
      String message = dataMap.get("description"); 
     } 
    } 
} 
相關問題