问题描述
我正在从 google firebase 为我的 vpn free Android 应用程序发送推送通知,目标为 Android 5.0:
I am sending push notification from google firebase for vpn下载 my android app with target of Android 5.0:
我的推送通知代码是:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
free vpn String badge = "0";
Uri uri = Uri.parse(
getString(R.string.app_host_name)
免费vpn );
Map<String, String> data = remoteMessage.getData();
vpn下载 if (data.size() > 0) {
免费vpn 免费vpn try {
免费vpn下载 uri = Uri.parse(
免费vpn data.get("link")
vpn下载 );
vpn下载 免费vpn badge = data.get("badge");
} 免费vpn catch (NullPointerException e) {
免费vpn下载 //
}
}
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);
免费vpn intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (Patterns.WEB_URL.matcher(url).matches()) {
intent.putExtra("link", url);
}
vpn下载 PendingIntent pendingIntent = PendingIntent.getActivity(
free vpn vpn下载 this,
免费vpn下载 0,
intent,
vpn下载 vpn free 免费vpn下载 PendingIntent.FLAG_UPDATE_CURRENT
免费vpn下载 );
Resources resources = getApplicationContext().getResources();
NotificationCompat.Builder notificationBuilder =
免费vpn下载 免费vpn下载 free vpn new NotificationCompat.Builder(this, "default")
免费vpn下载 免费vpn .setColor(
resources.getColor(R.color.colorPrimaryDark)
vpn free 免费vpn )
free vpn .setSmallIcon(
R.drawable.ic_stat_icon
vpn free )
vpn下载 免费vpn下载 .setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setNumber(Integer.parseInt(badge))
.setLargeIcon(
vpn下载 免费vpn BitmapFactory.decodeResource(
vpn下载 vpn下载 resources,
vpn下载 vpn下载 免费vpn vpn free 免费vpn vpn下载 vpn free R.mipmap.ic_launcher
免费vpn )
)
免费vpn下载 .setContentIntent(pendingIntent);
NotificationManager notificationManager =
free vpn free vpn (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= 26) {
免费vpn NotificationChannel 免费vpn notificationChannel = new NotificationChannel(
免费vpn 免费vpn free vpn "default",
vpn下载 free vpn "Main notification channel",
vpn free vpn下载 vpn free NotificationManager.IMPORTANCE_HIGH
);
notificationManager.createNotificationChannel(
vpn下载 notificationChannel
免费vpn );
}
notificationManager.notify(
vpn free 1,
免费vpn free vpn 免费vpn notificationBuilder.build()
vpn下载 );
}
当应用程序处于活动/打开/不在后台时,一切都非常完美,但是当它不是时,通知不会分组,没有显示数字,并且对所有这些设置都没有反应,我是什么只能通过清单设置更改小图标和圆圈颜色
And everything is super perfect 免费vpn vpn下载 when application is active/opened/not in free vpn 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 free vpn change is only small icon and circle color 免费vpn下载 via manifest settings
免费vpn <meta-data
vpn下载 android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_icon" />
<meta-data
免费vpn android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorPrimaryDark" vpn下载 />
但是为什么呢?就像当应用程序处于后台时,通知没有使用 Activity 代码中的设置,而是仅使用 AndroidManifest 中的某种默认"设置.
but vpn free why? It's like when app is in background notifications are not using settings from Activity code free vpn but using only some kind of "default" one from AndroidManifest.
推荐答案
正如你在评论中所说:
当应用处于后台时,应用不采用 setNumber、setAutoCancel、setSmallIcon、setLargeIcon 选项
这是因为您使用通知负载发送仅在前台触发的通知.
It is because you are using notification payload to send the notification which only triggers on the 免费vpn下载 foreground.
所以当你的应用在后台时它不会进入这个方法.
So when your app is in the background it does not enter this method.
要解决这个问题,您可以单独使用 data 负载:
to solve 免费vpn this you can use data payload alone:
"data": {
"titles": "New Title",
"bodys": "body here"
}
因为数据负载将进入 onMessageReceived() 当你的应用在前台/后台时.
since the data payload will enter the onMessageReceived() when your app is in foreground/background.
然后在 免费vpn fcm 中你可以这样做:
then in fcm you can vpn下载 do this:
免费vpn if (remoteMessage.getData().size() vpn下载 > 0) {
vpn free title = remoteMessage.getData().get("titles");
vpn下载 body = remoteMessage.getData().get("bodys");
vpn free }
这篇关于应用程序在后台时的 Android 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司免费vpn源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账免费vpn织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)