Android 通知大视图 - 未触发按钮上的 Intent

Android notification Big View - Intent on button not fired

我正在按照 Android Dev 教程使用 Big View 进行通知。我想要一个 on going 通知,它在单击通知正文时具有某些功能,而在单击按钮时具有其他功能。 这是我的代码:

Intent bodyIntent = new Intent(context, MainActivity.class);
innerIntent.putExtra(NOTIFICATION_BODY, "Click on notification body");  
PendingIntent bodyPendingIntent = PendingIntent.getActivity(context, 0, bodyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Intent buttonIntent = new Intent(context, MainActivity.class);
buttonIntent.setAction(NOTIFICATION_BUTTON);
PendingIntent buttonPendingIntent = PendingIntent.getService(context, 0, buttonIntent, 0);

android.support.v4.app.NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("My notification")
                .setContentText("Hello World!")
                .setCategory(Notification.CATEGORY_SERVICE)
                .setPriority(Notification.PRIORITY_MAX)
                .setVisibility(Notification.VISIBILITY_PRIVATE)
                .setContentIntent(notificationPendingIntent)
                .setAutoCancel(true)
                .setOnlyAlertOnce(true)
                .setLights(getResources().getColor(R.color.primary), 50, 10000)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setVibrate(new long[] {0, 50})
                .setOngoing(true)
                .setStyle(new NotificationCompat.BigTextStyle().bigText("Hello World BIG!"))
                .addAction(R.mipmap.ic_image, "Button", buttonPendingIntent);
int mNotificationId = 001;
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());

然后在onResume:

    Intent intent = getIntent();
    Log.e("m", String.valueOf(intent.getIntExtra(NOTIFICATION_RESULT, 0)));
    Log.e("m", String.valueOf(intent.getAction()));

结果:

单击通知正文时,bodyIntent 触发,我得到正确的 log 打印。

单击按钮时:没有任何反应,MainActivity甚至没有启动。

谢谢,

如果您想启动一个 activity,您应该调用 PendingIntent.getActivity() 方法,但您正在通过调用 PendingIntent.getService().

创建 buttonPendingIntent