单击"通知"是否未启动预期活动?

use*_*455 25 android android-notifications google-cloud-messaging

我在我的应用程序中使用GCM,并且每当收到GCM消息时都使用NotificationManager创建通知.现在一切正常,GCM消息在通知区域正确显示,但是当我点击通知时它应该启动一个活动我的应用程序将显示未发生的消息详细信息.每次我点击通知它都不会启动任何活动,它仍然保持原样.我创建通知的代码是:

private void sendNotification(String msg) {
        SharedPreferences prefs = getSharedPreferences(
                DataAccessServer.PREFS_NAME, MODE_PRIVATE);
        mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, WarningDetails.class);
        Bundle bundle = new Bundle();
        bundle.putString("warning", msg);
        bundle.putInt("warningId", NOTIFICATION_ID);
        intent.putExtras(bundle);
        // The stack builder object will contain an artificial back stack for
        // the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(WarningDetails.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(intent);

        PendingIntent contentIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.weather_alert_notification)
                .setContentTitle("Weather Notification")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg);
        String selectedSound = prefs.getString("selectedSound", "");
        if (!selectedSound.equals("")) {
            Uri alarmSound = Uri.parse(selectedSound);
            mBuilder.setSound(alarmSound);

        } else {
            Uri alarmSound = RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            mBuilder.setSound(alarmSound);
        }

        if (prefs.getBoolean("isVibrateOn", false)) {
            long[] pattern = { 500, 500, 500, 500, 500, 500, 500, 500, 500 };
            mBuilder.setVibrate(pattern);
        }

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
Run Code Online (Sandbox Code Playgroud)

我更新了我的代码以支持Preserving Navigation when Starting an Activity就像它在使用Android开发者网站的Gmail应用程序中发生的那样从那时起它停止了工作.有人请指导我在这段代码中缺少或做错了什么.

use*_*455 42

我的问题解决了我只需要添加PendingIntent.FLAG_ONE_SHOT标志,所以我更换了:

PendingIntent contentIntent = stackBuilder
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)

PendingIntent contentIntent = stackBuilder
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
                        | PendingIntent.FLAG_ONE_SHOT);
Run Code Online (Sandbox Code Playgroud)


Ton*_* Vu 23

我遇到了同样的问题并通过将android:exported ="true"添加到AndroidManifest.xml中的activity声明来解决它.

  • 你能解释一下为什么会这样吗?看来这个答案只是猜测.证明我是错的. (2认同)