Android 通知自定义视图:应用终止时无法点击自定义视图中的按钮
Android notifications custom view: cannot click on the button in the custom view when the app terminated
我为通知生成器分配了一个自定义视图,并在自定义视图中设置了按钮点击的待定意图,但是当应用不是 运行 时,无法点击按钮!
我的实现:
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());
我为通知生成器分配了一个自定义视图,并在自定义视图中设置了按钮点击的待定意图,但是当应用不是 运行 时,无法点击按钮!
我的实现:
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());