我正在使用GCM创建通知,当用户点击通知时,我将他重定向到一个Activity(CandyInfoActivity) .
当用户从以下位置点击通知时,此方案可以正常运行:
1.)应用程序中的任何位置(打开应用程序时)
2.)app在后台(按HOME按钮时)
3.)app关闭(按BACK <按钮)
现在的问题是:
当我从最近的APPS堆栈中删除应用程序(通过按最后一个剩余按钮清除最近的应用程序)时,我无法启动所需的活动,而是启动DEFAULT活动(MainActivity)
有趣(或我的哑)部分是当用户点击通知并且在问题场景中启动MainActivity时,则:
如果我按下BACK按钮,应用程序将关闭,并且当USER点击NOTIFICATION时需要启动 app is opened from the RECENT APPS, selection it shows the Desired activity .
我使用以下代码发送通知:
NotificationManager notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
final Intent intent = new Intent(this, CandyInfoActivity.class);
intent.putExtra(Constants.EXTRA_MESSAGE_DATA, cool);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0 /* Request code */, intent, 0);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(((BitmapDrawable) this.getResources().getDrawable(R.mipmap.ic_launcher)).getBitmap())
.setContentTitle(getString(R.string.app_name))
.setWhen(System.currentTimeMillis())
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setAutoCancel(true)
.setSound(defaultSoundUri);
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire(15000);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
因此,如果你理解了这个场景,我会在应用程序处于前台或后台时正确显示预期的活动,但是当我杀死(或者最好从最近的应用程序堆栈中删除)点击应用程序和通知时,预期的CandyInfoActivity.class是而不是打开MainActivity.class打开,这是应用程序的DEFAULT活动 .
P.S:我也注意到我的应用程序在某段时间内变为白色,而在此期间,CandyInfoActivity.class中使用/设置的日志将打印在控制台上并且MainActivity.class正在打开 . 我一直在这里搜索过去一周,所以如果你已经完成了这个问题(或)有任何建议,请在这里发帖 .
- 赛 -
1 回答
首先,确保正确设置XML中活动的层次结构 .
然后,构建应用程序堆栈并从
setContent
中的通知传递pendingIntent
. 这样,在使用通知的目标活动之后,用户将能够平滑地浏览应用程序 . 该片段显示the docs中建议的实现,您可以在其中找到详细说明.