Android 在与PendingEvent相同的活动中选择通知,但查看页面不同
场景: 我的应用程序有一个活动,其中包含一个ViewPager,显示两个片段(称为a页和B页) 问题: 当后台事件触发页面a的通知,且用户在页面B上时,选择通知不起任何作用;i、 e.没有对onCreate()、onResume()、onNewIntent()、onResumeFragments()或onStart()的回调,用户停留在页面B上 问题:Android 在与PendingEvent相同的活动中选择通知,但查看页面不同,android,Android,场景: 我的应用程序有一个活动,其中包含一个ViewPager,显示两个片段(称为a页和B页) 问题: 当后台事件触发页面a的通知,且用户在页面B上时,选择通知不起任何作用;i、 e.没有对onCreate()、onResume()、onNewIntent()、onResumeFragments()或onStart()的回调,用户停留在页面B上 问题: // Create a notification final Context ctx = this; Intent notifi
// Create a notification
final Context ctx = this;
Intent notificationIntent = new Intent(ctx, ActivityMain.class);
notificationIntent.putExtra(INTENT_EXTRA_NOTIFICATION, true);
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = ctx.getResources();
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, REQUESTCODE_ACTIVITYMAIN_LISTFRAGMENT, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder builder = new Notification.Builder(ctx);
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)//required
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setContentTitle(res.getString(R.string.notification_title))//required
.setContentText(data.description)//required
.setTicker(res.getString(R.string.notification_title))
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
.setAutoCancel(true);
Notification notification = builder.build();
notificationManager.notify(mNotificationID, notification);
- 当用户使用ViewPager时,如何使ViewPager显示页面 是否已选择通知并在B页
- 是否可以在活动中检测用户何时 是否选择了指向同一活动的通知
// Create a notification
final Context ctx = this;
Intent notificationIntent = new Intent(ctx, ActivityMain.class);
notificationIntent.putExtra(INTENT_EXTRA_NOTIFICATION, true);
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = ctx.getResources();
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, REQUESTCODE_ACTIVITYMAIN_LISTFRAGMENT, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder builder = new Notification.Builder(ctx);
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)//required
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setContentTitle(res.getString(R.string.notification_title))//required
.setContentText(data.description)//required
.setTicker(res.getString(R.string.notification_title))
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
.setAutoCancel(true);
Notification notification = builder.build();
notificationManager.notify(mNotificationID, notification);
可能有一个标志/配置可以添加到通知意图中,以强制活动识别所选的通知?这需要一些努力,但以下是为已在前台运行的活动选择通知时切换ViewPager选项卡的解决方案: 1)使用标志
flag\u ACTIVITY\u NEW\u TASK
创建通知,并额外显示通知启动了此活动
// Create a notification
final Context ctx = this;
Intent notificationIntent = new Intent(ctx, ActivityMain.class);
notificationIntent.putExtra(INTENT_EXTRA_NOTIFICATION, true);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//see rest of code in my question to create the notification
<activity
android:name="com.cool.app.ActivityMain"
android:label="@string/app_name"
android:launchMode="singleTop" >
</activity>
2)使用活动的singleTop
更新清单
// Create a notification
final Context ctx = this;
Intent notificationIntent = new Intent(ctx, ActivityMain.class);
notificationIntent.putExtra(INTENT_EXTRA_NOTIFICATION, true);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//see rest of code in my question to create the notification
<activity
android:name="com.cool.app.ActivityMain"
android:label="@string/app_name"
android:launchMode="singleTop" >
</activity>
4)在ActivityMain
(或任何您的活动)中覆盖onResume(),并包含切换选项卡的逻辑
@Override
protected void onResume()
{
super.onResume();
// Test if a Notification caused this activity to launch - if so then select the first tab
// NOTE: we get here from a notification by using Intent.FLAG_ACTIVITY_NEW_TASK
Bundle extras = getIntent().getExtras();
if (extras != null)
{
final boolean bIsLaunchedFromNotification = extras.getBoolean(INTENT_EXTRA_NOTIFICATION);
if (bIsLaunchedFromNotification)
{
final ActionBar actionBar = getActionBar();
actionBar.setSelectedNavigationItem(0);
}
}
}
5)对于良好形式-在ActivityMain(或应用程序)中,定义常数以传递额外值
final public static String INTENT_EXTRA_NOTIFICATION = "fromNotification";