android中的onMessageReceived在后台应用时没有调用

时间:2017-12-03 11:43:58

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

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

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

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

O中有一些新规则吗?怎么修好?谢谢!

更新

这个问题不是关于通知,而是关于firebase message srvice!

无论如何,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)
}

3 个答案:

答案 0 :(得分: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,则还需要更新到支持库的测试版:https://developer.android.com/topic/libraries/support-library/revisions.html#26-0-0-beta2(以便能够在compat构建器上调用setChannelId)。

小心,因为此库更新会将minSdkLevel提升为14。

答案 1 :(得分: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());

答案 2 :(得分:-1)

看起来问题出现在我的加氢试剂中。当我放下它并彻底刷新手机时 - 它可以正常工作。