屏幕锁定时PendingIntent不启动所需的活动

时间:2016-10-26 19:10:13

标签: android android-notifications android-pendingintent

我关注了doc for notificationsgcm example 当设备处于唤醒状态时,一切正常,通知声音,振动和所需的活动按预期工作,但当设备锁定时,这些都不能正常工作。

即使应用程序处于打开状态或处于后台状态,通知也会打开主要活动,而不是所需的活动。 这是我的代码:

JSONObject request=messageContent.getJSONObject("request");
intent = new Intent(getApplicationContext(), NewRequest.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
intent.putExtra(NewRequest.REQUEST, String.valueOf(request));

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


PendingIntent pendingIntent = PendingIntent.getActivity(this, id, intent,
       PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentText(body)
        .setContentTitle(title)
        .setAutoCancel(true)
        .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
        .setVibrate(new long[] { 1000, 1000 })
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntent);

Notification notification = notificationBuilder.build();
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;

NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "My Tag");
wl.acquire(10000);

notificationManager.notify(id, notification);

还尝试使用此方法保留导航流以用于挂起的意图,但结果是相同的。

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(NewRequest.class);
stackBuilder.addNextIntent(intent);

PendingIntent pendingIntent =
        stackBuilder.getPendingIntent(id, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

我不知道为什么只有在屏幕未锁定时才能起作用

0 个答案:

没有答案