首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当应用程序处于后台时Android通知

问当应用程序处于后台时Android通知
EN

Stack Overflow用户
提问于 2018-01-17 12:37:56
回答 1查看 4.3K关注 0票数 2

我正在从google为我的android应用程序发送推送通知,目标是Android5.0:

我的推送通知代码是:

代码语言:javascript
复制
@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()
    );
}

当应用程序处于活动/打开/不处于后台时,一切都是非常完美的,但如果不是,通知就不会分组,没有显示数字,并且对所有这些设置都没有任何反应,我能够更改的只是通过显式设置的小图标和圆圈颜色。

代码语言:javascript
复制
    <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的某种“默认”设置。

EN

回答 1

Stack Overflow用户

发布于 2018-01-17 13:05:08

正如你在评论中所说:

当应用程序处于后台时,应用程序不使用setNumber、setAutoCancel、setSmallIcon、setLargeIcon选项

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

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

要解决这个问题,您可以单独使用data有效负载:

代码语言:javascript
复制
"data": {
"titles": "New Title",
"bodys": "body here"
}

因为当您的应用程序处于前台/后台时,数据有效载荷将进入onMessageReceived()。

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

代码语言:javascript
复制
  if (remoteMessage.getData().size() > 0) {

        title = remoteMessage.getData().get("titles");
        body = remoteMessage.getData().get("bodys");
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48301350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档