这是我需要通知的情况:通知到达时,应用可能是前台,后台或已关闭
1.对于前景:当用户点击通知时,应用程序应显示“主要活动”。
2.对于背景:当用户点击通知时,应用程序应排在最前面并显示主要活动
3.关闭应用程序时:当用户点击通知时,应用程序应打开,并显示主要活动。
当我使用设置意图和Intent.FLAG_ACTIVITY_SINGLE_TOP
并使用设置意图时PendingIntent.FLAG_ONE_SHOT
。它可以按预期工作,但是当用户点击通知时,它不会调用Main Activity的onCreate方法。
private void showNotification() {
Intent intent = new Intent(this, DashboardActivity.class);
intent.putExtra(AppConstants.SCREEN, screen);
intent.putExtra(AppConstants.USER_ID, user_id);
intent.putExtra(AppConstants.TABLE_ID, table_id);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(getNotificationIcon())
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
如果您考虑上述代码,则需要从MainActivity(DashboardActivity)
用户点击通知时开始执行该方法。
注意:当应用程序是前台/背景时,它不应关闭并打开新的。它必须在现有应用程序中进行更新。
希望你了解我的情况。提前致谢。
当用户点击具有意图的通知时"Intent.FLAG_ACTIVITY_SINGLE_TOP"
,将不会调用意图的onCreate方法。当用户点击通知时,它将调用一个方法,如下所示:
@Override
protected void onNewIntent(Intent intent) {}
感谢您阅读此问题:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句