在安卓O系统中,当应用在后台运行时,onMessageReceived方法未被调用。

4
我正在从我的服务器发送数据负载通知,以下是一个示例:
url= "https://fcm.googleapis.com/fcm/send"
{
  "to" : "userToken",
  "data" : {
    //some json here
  }
}

通过这种方式,我可以成功地向用户发送消息,即使应用程序没有在所有Android O之前的设备上运行。

但是,在Android O设备上,当应用程序未启动时,onMessageReceived不会被调用...

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

更新

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

无论如何,Android O的通道也已经实现:

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

你找到任何解决方案了吗? - dakshbhatt21
看起来问题出在我的一加氢气体测试版上。当我将其重置并完全刷新手机时,它可以正常工作。 - Andriy Antonov
@AndriyAntonov 好的,太棒了,我也会尝试并发布更新。 - dakshbhatt21
@dakshbhatt21 你已经尝试过了吗? - Andriy Antonov
@AndriyAntonov,你解决了这个问题吗?我发现使用JobSheduler可以执行后台任务,但我不知道如何操作。https://developer.android.com/about/versions/oreo/background如果在Android O中杀死应用程序,则不允许运行应用程序的后台进程,我也遇到了这个问题,如果你找到了解决方案,请告诉我。 - Parvindra Singh
显示剩余2条评论
3个回答

0

在使用之前,您需要创建通知渠道。

 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,则还需要升级到支持库的 beta 版本:https://developer.android.com/topic/libraries/support-library/revisions.html#26-0-0-beta2(以便能够在兼容建造者上调用 setChannelId)。

请注意,此库升级会将 minSdkLevel 提高到 14。


谢谢,但我已经实现了通道。但是仍然不起作用。 - Andriy Antonov

0
从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());

抱歉,忘了提到这一点:通道已经实现,但仍然无法工作... - Andriy Antonov

-1

看起来问题出在我的一加氢气体测试版上。当我将其重置并完全刷新手机时,它可以正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接