我正在关注 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
/
关于Android 通知 Big View - Intent on button not fired,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34314563/