当应用程序被杀死时,onMessageReceived()不会被调用吗? [英] onMessageReceived() not getting called when app is killed?

查看:38
本文介绍了当应用程序被杀死时,onMessageReceived()不会被调用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase向我的应用发送推送通知.我的应用程序一直支持到API级别27.我正在使用FirebaseMessagingService接收通知.在onMessageReceived()方法内部,我要获取数据有效负载并在任务栏中创建自己的通知.从控制台,我仅发送数据有效负载,因为如果应用程序在后台,我不希望应用程序创建系统通知.现在,当应用程序处于前台和后台时,我可以在onMessageReceived()中获得回调.但是问题是当从内存中清除(或终止)应用程序时,它没有被调用.我的应用被杀死后,如何在onMessageReceived中获取回调?

I am using Firebase to send push notifications to my app. My app supports till API level 27.I am using FirebaseMessagingService to receive notifications. Inside onMessageReceived() method I am taking data payload and creating own notification in tray. From console I am sending only data payload since i dont want app to create system notifications if the app is in background. Now I am able to get callback in onMessageReceived() when app is in foreground and background. But the issue is its not getting invoked when the app is cleared from the memory(or killed). How can I get callback in onMessageReceived when my app is killed?

Manifest.xml

Manifest.xml

 <service android:name=".service.MyFirebaseInstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>

    <service android:name=".service.FcmMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="promotions" />

FcmMessagingService.java

FcmMessagingService.java

public class FcmMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    if (remoteMessage == null)
        return;

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

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Logger.v(Logger.TAG, "Data Payload: " + remoteMessage.getData().toString());

        try {
            //JSONObject json = new JSONObject(remoteMessage.getData().toString());
            NotificationHandler.handleNotification(this, formNotificationObject(null));
        } catch (Exception e) {
            Logger.e(Logger.TAG, "Exception: " + e.getMessage());
        }
    }
}

private Notification formNotificationObject(JSONObject data) {
   //creating notifcation object here
  }

build.gradle

build.gradle

compile "com.google.firebase:firebase-messaging:11.4.2"
compile 'com.facebook.android:facebook-android-sdk:4.29.0'
compile "com.android.support:appcompat-v7:26.0.0"

有效载荷

{
  "to": "cogzTKfWsXI:APA9................plSjNu",
  "mutable_content": false,
  "time_to_live": 20,
  "data": {
   "messageid": 10,
   "title": "Test",
   "body": "Testing",
   "image": "",
   "expiry": "2018-11-13 11:35:11"
  }

}

推荐答案

如果要在"Firebase控制台-通知撰写器"中发送消息,则该消息始终在前台使用.

If you are sending the Message in "Firebase Console - Notifications composer", it always used in foreground.

有两种类型的消息.

  1. 通知消息
  2. 数据消息

对于通知消息,仅在前台处理.但是,如果是数据消息,则可以在后台进行处理.

In case of Notification Message, it is handled just in Foreground. But in case of Data Message, it can be handled in Background.

并通过"Firebase控制台-通知"发送消息,表示消息类型为通知消息".

And sending the message through "Firebase console - Notification", means that the message type is "Notification Message".

如果要发送数据消息类型,则应使用Firebase Cloud Functions或其他功能.

If you want to send the type of Data Message, you should use Firebase Cloud Functions or others.

您可以检查以下链接: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

You can check following link: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

此链接: https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages

这篇关于当应用程序被杀死时,onMessageReceived()不会被调用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆