在关闭的应用程序中收到通知时转到特定 activity?
Go to specific activity when notification received in closed application?
下面是我的代码,它在应用程序处于后台时运行良好,但当应用程序不在后台时它会进入启动画面;
Intent resultIntent = null;
if (flag.equalsIgnoreCase("refer_friend")) {
resultIntent = new Intent(getApplicationContext(), MyLoyaltyHistoryActivity.class);
resultIntent.putExtra("key", "2");
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(getApplicationContext());
taskStackBuilder.addNextIntentWithParentStack(resultIntent);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(),
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder = new NotificationCompat.Builder(getApplicationContext());
mBuilder.setSmallIcon(R.drawable.yu_android);
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.yu_android);
} else {
mBuilder.setSmallIcon(R.drawable.yu_android);
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_MAX;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
如果您的应用程序不在后台,那么您可以获取在数据负载中发送的 JSON 数据和通知,如下所示:
只需在您的 SplashActivity.java:
中添加这行代码
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String pushType = bundle.getString("push_type");
在 bundle 中,您将获得从后端发送的所有键值数据。在我的例子中,我在数据有效负载中发送 push_type 密钥,在我的例子中收到的密钥你也可以在 SplashActivity.java。
最后我用 TaskStackBuilder
得到了答案
使用下面的代码;
Intent resultIntent = null;
if (flag.equalsIgnoreCase("refer_friend")) {
resultIntent = new Intent(getApplicationContext(), ReferFreindLoyaltyActivity.class);
resultIntent.putExtra("back_flag", "1");
resultIntent.putExtra("refer_a_freind", "1");
}
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(getApplicationContext());
taskStackBuilder.addNextIntentWithParentStack(resultIntent);
PendingIntent resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder = new NotificationCompat.Builder(getApplicationContext());
mBuilder.setSmallIcon(R.drawable.yu_android);
mBuilder.setContentTitle(title)
.setContentText(message)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent)
.setAutoCancel(true);
mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.yu_android);
} else {
mBuilder.setSmallIcon(R.drawable.yu_android);
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_MAX;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
下面是我的代码,它在应用程序处于后台时运行良好,但当应用程序不在后台时它会进入启动画面;
Intent resultIntent = null;
if (flag.equalsIgnoreCase("refer_friend")) {
resultIntent = new Intent(getApplicationContext(), MyLoyaltyHistoryActivity.class);
resultIntent.putExtra("key", "2");
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(getApplicationContext());
taskStackBuilder.addNextIntentWithParentStack(resultIntent);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(),
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder = new NotificationCompat.Builder(getApplicationContext());
mBuilder.setSmallIcon(R.drawable.yu_android);
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.yu_android);
} else {
mBuilder.setSmallIcon(R.drawable.yu_android);
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_MAX;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
如果您的应用程序不在后台,那么您可以获取在数据负载中发送的 JSON 数据和通知,如下所示:
只需在您的 SplashActivity.java:
中添加这行代码 Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String pushType = bundle.getString("push_type");
在 bundle 中,您将获得从后端发送的所有键值数据。在我的例子中,我在数据有效负载中发送 push_type 密钥,在我的例子中收到的密钥你也可以在 SplashActivity.java。
最后我用 TaskStackBuilder
使用下面的代码;
Intent resultIntent = null;
if (flag.equalsIgnoreCase("refer_friend")) {
resultIntent = new Intent(getApplicationContext(), ReferFreindLoyaltyActivity.class);
resultIntent.putExtra("back_flag", "1");
resultIntent.putExtra("refer_a_freind", "1");
}
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(getApplicationContext());
taskStackBuilder.addNextIntentWithParentStack(resultIntent);
PendingIntent resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder = new NotificationCompat.Builder(getApplicationContext());
mBuilder.setSmallIcon(R.drawable.yu_android);
mBuilder.setContentTitle(title)
.setContentText(message)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent)
.setAutoCancel(true);
mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.yu_android);
} else {
mBuilder.setSmallIcon(R.drawable.yu_android);
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_MAX;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());