Android 接收推送通知并在前台或后台处理它
Android receiving push notification and handling it either in foreground or background
我可以使用 FCM 服务向我的应用程序发送推送通知。我正在使用 POSTMAN/INSOMNIA.
一切正常。我可以看到通知。但是在处理传入消息时,我需要一些建议/指出正确的方向。
我已经使用 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
}
}
到目前为止没有什么特别的。
我想要实现的是,如果我的 Activity 在前台,我不需要显示任务栏通知,只是显示我的 FCM 消息附带的消息(my_message 键))在我的 Activity 里面。这可能吗?
如果应用程序在后台,我想打开一个 Activity(我可以这样做)并将我的数据传递给 Intent(上面的消息)这就是我挣扎。
如果'App in Background'场景发生,我的通知是这样显示的:
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());
}
所以我的 3 个主要问题是:
- 当通知到达时,我可以判断我的应用程序是否在 foreground/background 中吗?
- 如果在前台,我如何访问来自 activity
的数据
- 如果在后台,我如何将数据传递给 activity
感谢您的任何建议
要让您的应用了解传入数据,请注册一个与您的 activity 生命周期相关的 BroadcastReceiver
:看一下 Here
如果您的应用程序在后台,您可以根据您的用例将其存储在数据库/SharedPreferences 中,并在每次打开应用程序时检查它是否存在。
关于确定您的应用是否在 FG 中:
如果它在前台,请发送您的 Activity 必须接收的自定义广播。
如果它在后台,请存储它,并在下次打开应用程序时处理它。
我可以使用 FCM 服务向我的应用程序发送推送通知。我正在使用 POSTMAN/INSOMNIA.
一切正常。我可以看到通知。但是在处理传入消息时,我需要一些建议/指出正确的方向。
我已经使用 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
}
}
到目前为止没有什么特别的。 我想要实现的是,如果我的 Activity 在前台,我不需要显示任务栏通知,只是显示我的 FCM 消息附带的消息(my_message 键))在我的 Activity 里面。这可能吗?
如果应用程序在后台,我想打开一个 Activity(我可以这样做)并将我的数据传递给 Intent(上面的消息)这就是我挣扎。
如果'App in Background'场景发生,我的通知是这样显示的:
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());
}
所以我的 3 个主要问题是:
- 当通知到达时,我可以判断我的应用程序是否在 foreground/background 中吗?
- 如果在前台,我如何访问来自 activity 的数据
- 如果在后台,我如何将数据传递给 activity
感谢您的任何建议
要让您的应用了解传入数据,请注册一个与您的 activity 生命周期相关的 BroadcastReceiver
:看一下 Here
如果您的应用程序在后台,您可以根据您的用例将其存储在数据库/SharedPreferences 中,并在每次打开应用程序时检查它是否存在。
关于确定您的应用是否在 FG 中:
如果它在前台,请发送您的 Activity 必须接收的自定义广播。
如果它在后台,请存储它,并在下次打开应用程序时处理它。