在后台运行应用程序时未调用android O中的onMessageReceived [英] onMessageReceived in android O not called when app in background

查看:132
本文介绍了在后台运行应用程序时未调用android O中的onMessageReceived的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从服务器发送数据有效负载通知.这是示例:

I am sending data payload notifications from my server. here is example:

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

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

in such way i am successfully sending messages to users, even if app isn't running, in all pre Android O devices. But on Android O device, onMessageReceived not called when app is not launched...

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

is there some new rules in O ? how it can be fixed? thanks!

更新

此问题与通知无关,而与Firebase消息服务有关!

This question is not about notifications, but about firebase message srvice!

无论如何,还可以实现Android O的频道:

Anyway, chanels for Android O is also implemented:

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

推荐答案

从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)或更低版本的设备上一样.

Starting in Android 8.0 (API level 26), notification channels allow you to create a user-customizable channel for each type of notification you want to display. Notification channels provide a unified system to help users manage notifications. When you target Android 8.0 (API level 26), you must implement one or more notification channels to display notifications to your users. If you don't target Android 8.0 (API level 26) but your app is used on devices running Android 8.0 (API level 26), your app behaves the same as it would on devices running Android 7.1 (API level 25) or lower.

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

这篇关于在后台运行应用程序时未调用android O中的onMessageReceived的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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