android - Android 通知自定义视图:应用终止时无法点击自定义视图中的按钮
问题描述
我为通知生成器分配了一个自定义视图,并为自定义视图中的按钮单击设置了待处理的意图,但是当应用程序未运行时,无法单击该按钮!
我的实现:
RemoteViews notificationLayout = new RemoteViews(getPackageName(),R.layout.emotions_notification);
Intent veryDissatisfiedIntent = new Intent(this, ReceiverService.class);
intent.setAction(EMOTIONS_VERY_DISSATISFIED_ACTION);
PendingIntent pendingIntentVeryDissatisfied = PendingIntent.getService(this, id, veryDissatisfiedIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationLayout.setOnClickPendingIntent(R.id.sentiment_very_dissatisfied, pendingIntentVeryDissatisfied);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channel_ID)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSound(uri)
.setAutoCancel(true);
mBuilder.setContent(notificationLayout);
mBuilder.setStyle(new NotificationCompat.DecoratedCustomViewStyle());
mBuilder.setCustomBigContentView(notificationLayout);
notificationManager.notify(id, mBuilder.build());
接收服务:
public int onStartCommand(Intent intent, int flags, int startId) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String action = intent.getAction();
Bundle extras = intent.getExtras();
int notificationId = extras.getInt("notificationId");
switch (action) {
case FirebasePluginMessagingService.EMOTIONS_VERY_DISSATISFIED_ACTION:
// do something
break;
}
return START_STICKY;
notificationManager.cancel(notificationId);
}
解决方案
我通过以下方式解决了这个问题:
startForeground(id, mBuilder.build());
推荐阅读
- android - Luxand API 在 android 上的相似性
- javascript - asyncdata 函数不在服务器端呈现 vuex 数据
- javascript - 通过 android 中的 javaScript 更改 webView 字体系列
- node.js - 在 express-handlebars 中包含布局问题
- c# - 为什么对象的 list<> 属性为空
- vue.js - 在VueJS中向列表添加多个元素时添加过渡动画
- python - 如何将像“xe4\xb8\xad”这样的str变量转换为中文?
- php - 如何安排 cron 作业以每天提取数据库并通过邮件发送报告
- python - 为什么有 %26 (Python)?
- jestjs - 使用 Babel 和 Jest 在测试中启动 Web API 文件对象时出现问题