【问题标题】:Intent does not work in Android notificationIntent 在 Android 通知中不起作用
【发布时间】:2014-08-05 06:56:49
【问题描述】:

我尝试发出通知,但点击通知没有按预期工作:在活动中我无法检索附加信息发送通知:

    int requestID = (int) System.currentTimeMillis();

    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(this, LoadPictureActivity.class);
    intent.putExtra("Test", "sometext");
    PendingIntent pIntent = PendingIntent.getActivity(this, requestID, intent, 0);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.common_signin_btn_icon_focus_light)
                    .setContentTitle("GCM Notification")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setContentText(msg)
                    .setContentIntent(pIntent)
                    .setAutoCancel(true);

    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

在 LoadPictureActivity 的 onCreate 方法中,我尝试获取其他信息:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent intent = getIntent();
    Log.d("App", "Intent get intent: " + intent);
    if (intent != null) {
        Log.d("App", "Intent extra sting: " + intent.getStringExtra("Test"));
    }
}

但是在单击通知后我没有看到任何带有附加字符串的输出,我也尝试在 OnResume 中进行相同的检查,但在这种情况下,单击通知后我看到这会触发原始启动主类的意图,该类可以为空额外的字符串。所以我不确定通知点击是否完全正常。问题出在哪里?

【问题讨论】:

  • 听起来你正在使用特殊的启动模式(如 singleTasksingleInstance,在这种情况下,当你点击 Notification 时,onCreate() 不会被调用全部。Android 正在恢复您的 Activity 的现有实例。

标签: android android-intent notifications


【解决方案1】:

试试这个方法

 PendingIntent pIntent = PendingIntent.getActivity(this, requestID, intent, PendingIntent.FLAG_UPDATE_CURRENT);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多