2017-03-16 295 views
5

我想在後臺以及前臺處理Firebase通知消息。我將發送一條消息,其中包含來自開發人員的YouTube鏈接,當用戶點擊通知欄時,必須指示用戶打開鏈接。有誰知道它是如何完成的?如何處理背景以及前景的Firebase通知?

public void onMessageReceived(RemoteMessage remoteMessage) { 
    // [START_EXCLUDE] 
    // There are two types of messages data messages and notification messages. Data messages are handled 
    // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type 
    // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app 
    // is in the foreground. When the app is in the background an automatically generated notification is displayed. 
    // When the user taps on the notification they are returned to the app. Messages containing both notification 
    // and data payloads are treated as notification messages. The Firebase console always sends notification 

    // [END_EXCLUDE] 

    // TODO(developer): Handle FCM messages here. 
    // Not getting messages here? See why this may be: 
    Log.d(TAG, "From: " + remoteMessage.getFrom()); 

    // Check if message contains a data payload. 
    if (remoteMessage.getData().size() > 0) { 
     Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
    } 

    // Check if message contains a notification payload. 
    if (remoteMessage.getNotification() != null) { 
     Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
    } 

    // Also if you intend on generating your own notifications as a result of a received FCM 
    // message, here is where that should be initiated. See sendNotification method below. 
} 

接下來做什麼來達到我的目標?在此先感謝:)

+0

發佈的代碼只是示例中的代碼。所以我假設你已經經歷過他們。有什麼困惑嗎?你是否也閱讀過文檔?現在,這就像一個*給我代碼*問題。 –

+0

試試我的解決方案?
我已經試過了。
它的工作
https://stackoverflow.com/questions/48897883/how-to-handle-notifications-with-fcm-when-app-is-in-e-fore-foreground-or-backgro/48899186#48899186 –

回答

4

您應該在您的FCM消息中發送數據有效載荷。無論您的應用處於前臺還是後臺,數據有效負載均以消息方式接收。在那裏處理行動。就像通過閱讀數據有效負載總是顯示通知一樣,或者如果您希望在應用程序處於打開狀態或前景狀態時顯示警告對話框。

這裏是一個示例有效載荷:

{ 
    "to": "registration_id_or_topic", 
    "data": { 
     "message": "This is a Firebase Cloud Messaging Topic Message!", 
     "youtubeURL": "https://youtu.be/A1SDBIViRtE" 
    } 
} 

然後在您的onMessageReceived:

public void onMessageReceived(RemoteMessage remoteMessage) { 
    if (remoteMessage.getData().size() > 0) { 
     Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
     Map<String, String> receivedMap = remoteMessage.getData(); 
     String youtubeURL = receivedMap.get("youtubeURL"); 
     showNotificationWithURLAction(youtubeURL); 
    } 
    ..... 
} 

可以方便地實現showNotificationWithURLAction(...)通過谷歌搜索出來的方法。一個樣品是here

+0

如何在有效載荷中發送鏈接以及如何在用戶單擊時處理該鏈接?請幫助我 –

+0

請通過@Lindani Masinga查看以下答案。我將更新我的答案以及 – drulabs

+0

要從控制檯發送數據有效載荷請參閱以下答案@ Drake29a – drulabs

0

如果你想將用戶重定向到的內容在您的應用程序比使用深層鏈接, 這裏是在點擊通知時鏈接如何只開放:

Intent notificationIntent = new Intent(Intent.ACTION_VIEW); 
     notificationIntent.setData(Uri.parse(remoteMessage.getData().getString("url")); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     // Resources r = getResources(); 
      Notification notification = new NotificationCompat.Builder(context) 
        .setTicker("yortext") 
        .setSmallIcon(android.R.drawable.ic_menu_report_image) 
        .setContentTitle("title") 
        .setContentText("content") 
        .setContentIntent(pendingIntent) 
        .build(); 

      NotificationManager notificationManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE); 
      notificationManager.notify(0, notification); 

要發送有效載荷進入火力控制檯,創建新消息,在這裏您將擁有高級選項,您可以將數據放入鍵/值對中,並將 url設置爲如下照片所示。

Adding payload to Firebase message

+0

請不要投下我的問題。 –

+0

這並不解決OP中提出的問題。 – drulabs

+0

德雷克接受我在fb上的朋友請求我想問一些關於firebase的疑問@ Drake29a –

1

這是this question可能重複。不要在推送消息正文中發送「通知」。

所以基本上,我改變了身體的推送通知要求從:

{ 
    "data": { 
     "type" : "mytype" 
    }, 
    "notification": { 
     "title": "My Title", 
     "body": "My Notification Message" 
    }, 
    "to": "/topics/all" 
} 

要:

{ 
    "data": { 
     "type" : "mytype", 
     "title": "My Title", 
     "body": "My Notification Message" 
    }, 
    "to": "/topics/all" 
} 

現在我的應用程序調用onMessageReceived(),即使在後臺每次,我只是改變了在推送數據中使用所獲得的通知標題和消息的方法。

+0

嗨,我的應用程序不會在後臺調用onMessageReceived()。 – Avi

0

android系統將始終處理有效內容中通知字段內的消息。如果您希望自己的應用處理通知,則需要將YouTube鏈接放入有效內容中的該數據字段中。

{ "data" : 
    { "link" : "www.youtube.com"} 
} 

此通知在應用程序中作爲RemoteMessage接收。使用remoteMessage.getData()來獲得youtube鏈接。

請參閱https://firebase.google.com/docs/cloud-messaging/concept-options#messages-with-both-notification-and-data-payloads

+0

嗨,我的應用程序不會在後臺調用onMessageReceived()。 – Avi