我可以用FCM服务向我的应用程序发送推送通知。我是利用邮递员/失眠症来做的。
一切都很好。我能看到通知。但是,在处理传入的消息时,我需要一些建议/指出正确的方向。
我已经设置了所有使用FCM正式文档。
我的服务器调用如下所示:
{
"to":"...",
"data" : {
"sound" : "default",
"body" : "Message body",
"title" : "My APP",
"content_available" : true,
"priority" : "high",
"my_message": "123456"
}
}我可以访问onMessageReceived()方法中的数据:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
// get incoming data
}
}到目前为止还没有什么特别之处。我想要实现的是,如果我的活动在前台,我不需要显示任务栏通知,只需要显示活动中FCM消息( my_message键)附带的消息。这有可能吗?
如果应用程序是在后台,我想打开一个活动(我能做)并传递给我的意图,我的数据(上面的信息),这是我正在挣扎。
如果“后台应用程序”场景发生,我的通知将以如下方式显示:
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "my_channel_id";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_person)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"DG_Channel",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}所以我的三个主要问题是:
谢谢你的建议
发布于 2020-10-08 14:14:11
https://stackoverflow.com/questions/64261351
复制相似问题