【问题标题】:onMessageReceived() not getting called when app is killed?应用程序被杀死时没有调用onMessageReceived()?
【发布时间】:2018-07-10 06:25:03
【问题描述】:

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

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" />

FCMessagingService.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

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"
  }

}

【问题讨论】:

  • 您在哪个设备上测试?
  • 我认为 onMessageReceived 不应该在应用被杀死时被调用,因为它是你应用中的代码并且你的应用被杀死
  • @sandeepkolhal 我正在测试一加五 Oxygen OS 5.0,奥利奥。
  • 你在其他手机上测试过吗?试试这个stackoverflow.com/questions/36035284/…
  • @sandeepkolhal 我只在一加五中进行测试,因为这是这里唯一可用的带有奥利奥操作系统的设备。

标签: android firebase push-notification firebase-cloud-messaging


【解决方案1】:

如果您在“Firebase 控制台 - 通知编写器”中发送消息,则它始终在前台使用。

有两种类型的消息。

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

在通知消息的情况下,它只在前台处理。 但如果是数据消息,则可以在后台处理。

通过“Firebase 控制台-通知”发送消息,表示消息类型为“通知消息”。

如果你想发送数据消息的类型,你应该使用 Firebase Cloud Functions 或其他。

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

还有这个链接:https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages

【讨论】:

  • 我只发送数据消息
  • 当应用程序在后台时我能够收到它
【解决方案2】:

Android 应用由不同的活动和服务组成,Android 区分应用的停止状态和活动的停止状态。当用户从最近的应用程序列表中将应用程序刷出内存时,活动将停止。另一方面,应用程序在以下情况下处于停止状态:

  1. 首次安装但尚未启动和
  2. 当它们被用户在管理应用程序中手动停止时source

问题

由于一些 Android 手机制造商误解了此规范(请参阅 thisthis),当从最近视图中滑动应用程序时将应用程序置于停止状态,似乎没有办法可靠地获得推送通知会发送到所有 Android 设备,除非您是 WhatsApp、Gmail、Twitter 等,并且您是 whitelisted by these manufacturers

修复

最终结果是,有许多特定于设备和特定于操作系统版本的因素会影响我们的应用推送通知的交付能力。尝试的事情:

  • 在 Android X 到 7.1 上,启用自动启动:设置 > 安全 > 权限 > 自动启动

  • 在 Android 8.0 上,自动启动选项已被删除,因此现在您可以转到设置 > 电池 > 电池优化 >(左上角的三个点菜单)高级优化 > 关闭高级优化

【讨论】:

  • Janne:Firebase 支持人员表示,是三星决定关闭服务以节省电池电量。他们要求三星修复它,但它没有修复。没有修复。
猜你喜欢
  • 1970-01-01
  • 2021-01-01
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
相关资源
最近更新 更多