在后台运行应用程序时未调用android

在后台运行应用程序时未调用android

本文介绍了在后台运行应用程序时未调用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)
}

推荐答案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 22:16