gpt4 book ai didi

java - 应用程序在后台时的 Android 通知

转载 作者:搜寻专家 更新时间:2023-11-01 08:23:05 28 4
gpt4 key购买 nike

我正在为目标为 Android 5.0 的 Android 应用程序发送来自 google firebase 的推送通知:

我的推送通知代码是:

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

当应用程序处于 Activity/打开/不在后台时,一切都非常完美,但当它不是时,通知不会分组,没有显示数字,所有这些设置都没有反应,我是只能通过 list 设置更改小图标和圆圈颜色

    <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" />

但是为什么呢?这就像当应用程序处于后台时,通知不使用 Activity 代码中的设置,而是仅使用 AndroidManifest 中的某种“默认”设置。

最佳答案

正如您在评论中所说:

when app is on background, app isn't taking setNumber, setAutoCancel, setSmallIcon, setLargeIcon options

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

所以当你的应用程序在后台时,它不会进入这个方法。

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

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

由于数据负载将进入onMessageReceived()当您的应用处于前台/后台时。

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

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

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

关于java - 应用程序在后台时的 Android 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48301350/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com