应用在后台时的Android通知 [英] Android notifications when app is in background

查看:192
本文介绍了应用在后台时的Android通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送谷歌firebase的推送通知,我的Android应用程序的目标是Android 5.0:

I am sending push notification from google firebase for my android app with target of Android 5.0:

我的推送通知代码是:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    String badge = "0";
    Uri uri = Uri.parse(
            getString(R.string.app_host_name)
    );

    Map<String, String> data = remoteMessage.getData();
    if (data.size() > 0) {
        try {
            uri = Uri.parse(
                    data.get("link")
            );

            badge = data.get("badge");
        } catch (NullPointerException e) {
            //
        }
    }

    if (remoteMessage.getNotification() != null) {
        RemoteMessage.Notification notification = remoteMessage.getNotification();
        sendNotification(notification.getTitle(), notification.getBody(), uri.toString(), badge);
    }
}



private void sendNotification(String title, String body, String url, String badge) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    if (Patterns.WEB_URL.matcher(url).matches()) {
        intent.putExtra("link", url);
    }

    PendingIntent pendingIntent = PendingIntent.getActivity(
            this,
            0,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
    );

    Resources resources = getApplicationContext().getResources();

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, "default")
                    .setColor(
                            resources.getColor(R.color.colorPrimaryDark)
                    )
                    .setSmallIcon(
                            R.drawable.ic_stat_icon
                    )
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setNumber(Integer.parseInt(badge))
                    .setLargeIcon(
                            BitmapFactory.decodeResource(
                                    resources,
                                    R.mipmap.ic_launcher
                            )
                    )
                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= 26) {
        NotificationChannel notificationChannel = new NotificationChannel(
                "default",
                "Main notification channel",
                NotificationManager.IMPORTANCE_HIGH
        );

        notificationManager.createNotificationChannel(
                notificationChannel
        );
    }

    notificationManager.notify(
            1,
            notificationBuilder.build()
    );
}

当应用程序处于活动状态/打开/不在后台时,一切都非常完美,但是当它没有,通知没有分组,没有显示数字,并且根本没有对所有这些设置做出反应,我能够改变的只是通过清单设置的小图标和圆形颜色

And everything is super perfect when application is active/opened/not in background, but when it is not, notifications are not grouped, there is no number displayed, and no reaction on all of this settings at all, what i was able to change is only small icon and circle color via manifest settings

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_stat_icon" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorPrimaryDark" />

但为什么?就像应用程序处于后台通知时不使用活动代码中的设置但只使用AndroidManifest中的某种默认设置。

but why? It's like when app is in background notifications are not using settings from Activity code but using only some kind of "default" one from AndroidManifest.

推荐答案

正如你在评论中所说:


当app在后台时,应用程序没有采用setNumber,setAutoCancel, setSmallIcon,setLargeIcon options

这是因为您使用通知有效负载发送仅在前台触发的通知。

It is because you are using notification payload to send the notification which only triggers on the foreground.

因此当你的应用程序在后台时,它不会输入此方法。

So when your app is in the background it does not enter this method.

要解决此问题,你可以单独使用数据有效负载:

to solve this you can use data payload alone:

"data": {
"titles": "New Title",
"bodys": "body here"
}

因为数据有效负载将进入 onMessageReceiv ed() 当你的应用处于前台/后台时。

since the data payload will enter the onMessageReceived() when your app is in foreground/background.

然后在fcm中你可以这样做:

then in fcm you can do this:

  if (remoteMessage.getData().size() > 0) {

        title = remoteMessage.getData().get("titles");
        body = remoteMessage.getData().get("bodys");
    }

这篇关于应用在后台时的Android通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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