当接收设备在Android中使用FCM处于前台状态时,如何从设备发送推送通知?

时间:2017-07-07 05:04:18

标签: android firebase firebase-cloud-messaging

我使用FCM创建了一对一聊天。我一度陷入困境,我想在设备未处于前台状态时发送推送通知。我是新手,任何人都可以建议我如何进行。 三江源

1 个答案:

答案 0 :(得分:0)

您可以在MyFirebaseMessagingService类中尝试以下代码:

 if (remoteMessage.getData() != null && remoteMessage.getData().size() > 0) {
            Log.d("TAGRK", "Message data payload: " + remoteMessage.getData());

            message = "" + remoteMessage.getData().get(Config.MESSAGE_KEY);//MESSAGE_KEY is your key for message in your json.(eg. "message")
            title = "" + remoteMessage.getData().get(Config.TITLE_KEY);//TITLE_KEY is your key for title in your json..(eg. "title")
            //send pushnoification method
        } else if (remoteMessage.getNotification() != null) {
            Log.d("TAGRK", "Message Notification Body: " + remoteMessage.getNotification().getBody());
            message = "" + remoteMessage.getNotification().getBody();
            title = "" + remoteMessage.getNotification().getTitle();
             //send pushnoification method
        }