2017-03-16 85 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