【发布时间】:2018-07-05 06:18:15
【问题描述】:
让我和你讨论一个场景,让应用程序在前台打开 Activity “X”。现在我的应用通过 Firebase 消息服务收到了通知。 我想要的是? 我希望如果我的应用程序在前台,我的活动“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());
}
【问题讨论】:
标签: android push-notification notifications foreground