Android 通知 Big View - Intent on button not fired

标签 android android-notifications android-notification-bar

我正在关注 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/

相关文章:

android - Google map V2 - 授权失败,我无法显示 map

android - 如何在android中创建异步通知

android - 带有操作按钮的推送通知不起作用

android - 这个字段ScrollView中的foliage名称如何?

android - ViewPager 作为循环队列/包装

android - 避免在按下后退按钮时关闭警报对话框?

android - 如何为通知进行android单元测试?

android - 如何将文本颜色与通知颜色栏相匹配?

android - 从 AsyncTask 更新通知栏导致通知栏崩溃

Android 在收到消息时在状态栏中停止通知