【问题标题】:onMessageReceived in android O not called when app in backgroundandroid O中的onMessageReceived在后台应用时未调用
【发布时间】:2017-12-03 11:43:57
【问题描述】:

我正在从我的服务器发送数据有效负载通知。这是一个例子:

url= "https://fcm.googleapis.com/fcm/send"
{
  "to" : "userToken",
  "data" : {
    //some json here
  }
}

以这种方式,即使应用程序没有运行,我也可以在所有预 Android O 设备中成功地向用户发送消息。 但在 Android O 设备上,应用未启动时不会调用 onMessageReceived...

在 O 中有一些新规则吗?如何解决?谢谢!

更新

这个问题不是关于通知,而是关于 firebase 消息服务!

反正Android O的chanels也实现了:

val CHANEL_ID = "MY_CHANEL_ID"

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val channel = NotificationChannel(CHANEL_ID, "Channel human readable title", NotificationManager.IMPORTANCE_HIGH)
    (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(channel)
}

【问题讨论】:

  • 你在 manifest 中声明了服务类吗??
  • 当然。其他(前 O)设备成功接收到此消息
  • 你找到解决办法了吗?
  • @dakshbhatt21 看起来问题出在我的一加氢测试版中。当我放下它并彻底刷新手机时 - 它工作正常。
  • @AndriyAntonov 好的,我也会尝试并发布更新

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


【解决方案1】:

您需要先创建通知通道才能使用它。

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

      /* Create or update. */
      NotificationChannel channel = new NotificationChannel("my_channel_01",
          "Channel human readable title", 
          NotificationManager.IMPORTANCE_DEFAULT);
      mNotificationManager.createNotificationChannel(channel);
  }

此外,仅当您的 targetSdkVersion 为 26 或更高时,您才需要使用通道。

如果您使用的是 NotificationCompat.Builder,您还需要更新到支持库的测试版:https://developer.android.com/topic/libraries/support-library/revisions.html#26-0-0-beta2(以便能够在兼容构建器上调用 setChannelId)。

请小心,因为此库更新将 minSdkLevel 提高到 14。

【讨论】:

  • 谢谢,但我的 chanels 已经实现了。但还是不行
【解决方案2】:

从 Android 8.0(API 级别 26)开始,通知通道允许 您可以为每种类型的用户创建一个可自定义的频道 要显示的通知。通知渠道提供了一个 统一系统,帮助用户管理通知。当你瞄准 Android 8.0(API 级别 26),必须实现一个或多个 通知渠道向您的用户显示通知。如果你 不要以 Android 8.0(API 级别 26)为目标,但您的应用用于 运行 Android 8.0(API 级别 26)的设备,您的应用的行为相同 就像在运行 Android 7.1(API 级别 25)或更低版本的设备上一样。

https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ManageChannels

示例代码:

        // The id of the channel.
        String CHANNEL_ID = "my_channel_01";
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(MainActivity.this).setChannel(CHANNEL_ID)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("My notification")
                        .setContentText("Hello World!");
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, MainActivity.class);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your app to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(MainActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // mNotificationId is a unique integer your app uses to identify the
        // notification. For example, to cancel the notification, you can pass its ID
        // number to NotificationManager.cancel().
        mNotificationManager.notify(0, mBuilder.build());

【讨论】:

  • 对不起,忘了说:频道已经实现了,但还是不行……
【解决方案3】:

看来问题出在我的一加氢测试版上。当我放下它并彻底刷新手机时 - 它工作正常。

【讨论】:

    猜你喜欢
    • 2018-04-12
    • 2017-04-22
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    相关资源
    最近更新 更多