【发布时间】:2017-11-14 07:45:19
【问题描述】:
我正在寻找一种在应用程序运行、后台或被终止时处理 fcm 通知的模式。
我们的应用程序具有登录过程,因此当应用程序未启动时,通知需要打开启动器,但是当应用程序运行时,我只想在用户单击通知时推送一个活动。
所以我猜最好的方法是 ContentIntent,它要么被传递到堆栈中最顶层的 Activity,要么被传递到 Launch-Activity。
无论应用程序是否正在运行,我当前的代码总是使用 TaskStackBuilder 重新启动应用程序。
我的代码示例:
resultIntent = new Intent(this, MyProfileActivity.class);
resultIntent.putExtra(ActivityUtil.KEY_IMAGE_URL, SharedPref.getUserImage(this));
resultIntent.putExtra(KEY_NOTIFICATION_ID, notificationID);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(SetDetailReworked.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "...")
.setLargeIcon(getBitmapFromUrl(images[UI.GetDensityInt(this)]))
.setSmallIcon(R.drawable.bar_icon)
.setContentTitle(getString(R.string.push_notification_Title))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(resultPendingIntent);
Notification not = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID, not);
【问题讨论】:
标签: android firebase android-intent push-notification firebase-cloud-messaging