Android - 我的 Activity 如何检测到我在 Firebase 消息服务中收到通知?
Android - How can my Activity detect that I received a notification in Firebase Messaging Service?
跟大家讨论一个场景,让app在前台Activity "X"打开。现在我的应用程序通过 Firebase 消息服务收到了通知。
我想要的是?
我希望如果我的应用程序在前台,我的 activity "X" 可以检测到我是否收到通知,以便我可以切换图标。
这是我的 Firebase 消息服务代码:
@Override
public void onMessageReceived(RemoteMessage message){
type=message.getData().get("type");
CrucialData.setReceivedNotificationWithoutChecking(true);
createNotification(message);
}
private void createNotification(RemoteMessage message) {
Context context = getBaseContext();
checkingIfAppIsInForeground(context);
if(!inForeground){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.appicon)
.setColor(getResources().getColor(R.color.colorPrimaryDark))
.setContentTitle("Your chat is going to expire tomorrow!")
.setAutoCancel(true);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, main_activity.class);
resultIntent.putExtra("launchedFromNotification",true);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your app to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(main_activity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mNotificationId is a unique integer your app uses to identify the
mNotificationManager.notify(1231, mBuilder.build());
}
您可以使用 Local Broadcast 在您的服务和 activity 之间进行通信。
这是一个 example。
跟大家讨论一个场景,让app在前台Activity "X"打开。现在我的应用程序通过 Firebase 消息服务收到了通知。 我想要的是? 我希望如果我的应用程序在前台,我的 activity "X" 可以检测到我是否收到通知,以便我可以切换图标。
这是我的 Firebase 消息服务代码:
@Override
public void onMessageReceived(RemoteMessage message){
type=message.getData().get("type");
CrucialData.setReceivedNotificationWithoutChecking(true);
createNotification(message);
}
private void createNotification(RemoteMessage message) {
Context context = getBaseContext();
checkingIfAppIsInForeground(context);
if(!inForeground){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.appicon)
.setColor(getResources().getColor(R.color.colorPrimaryDark))
.setContentTitle("Your chat is going to expire tomorrow!")
.setAutoCancel(true);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, main_activity.class);
resultIntent.putExtra("launchedFromNotification",true);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your app to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(main_activity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mNotificationId is a unique integer your app uses to identify the
mNotificationManager.notify(1231, mBuilder.build());
}
您可以使用 Local Broadcast 在您的服务和 activity 之间进行通信。
这是一个 example。