我正在从google firebase为我的android应用程序发送推送通知,目标是Android5.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()
);
}
<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中的某种“默认”设置。
正如你在评论中所说:
当应用程序位于后台时,应用程序不使用setNumber、setAutoCancel、setSmallIcon、setLargeIcon选项
这是因为您正在使用通知负载来发送只在前台触发的通知。
"data": {
"titles": "New Title",
"bodys": "body here"
}
那么在fcm中可以这样做:
if (remoteMessage.getData().size() > 0) {
title = remoteMessage.getData().get("titles");
body = remoteMessage.getData().get("bodys");
}
有时当应用程序收到通知时,它不会发出声音。下面是代码段。
null 而当app在后台时,系统托盘总是显示一个到达一个重复的通知(如收到通知a,系统托盘显示2个通知a)。 怎么解决这个问题? 编辑:添加的代码 我扩展了 类,并在 方法中包含该类 这是项目中我使用NotificationManager的唯一部分。 另外,我尝试在这个方法上添加一个日志。当应用程序处于前台时调用onMessageReceived。当应用程序在后台时,它不会被调用
当应用程序在后台时,如何更新包含数据负载的Firebase推送通知?有没有办法在通知中指定通知id给Firebase API? 完整的项目在github https://github.com/akshatashan/FireBaseCloudMessagingDemo中
我正在使用FCM在代码下推送通知,以便在收到通知时播放声音 我称之为OnMessageReceived方法,但声音仅在应用程序位于前台时播放,而不是在应用程序位于后台时播放
我想为一个聊天应用程序实现FCM推送通知服务,我遵循Firebase文档中的步骤,当通知从Firebase控制台发送到我的设备时,我会得到通知。当我尝试使用http post to https://FCM.googleapis.com/FCM/send通过FCM将通知通过服务器端发送到设备时: 当应用程序处于活动状态并且我正在将此通知发送到我的设备时,Im在我的控制台日志中收到以下消息,所以我认为
我从控制台向我的应用程序发送消息,当它在后台时,我设法让应用程序做出我想要的反应。所以基本上SDK会显示一个通知,当用户点击它并启动应用程序时,我会使用一些自定义数据字段来构建对话框消息。