我为通知生成器分配了一个自定义视图,并在该自定义视图中设置了按钮单击的未决意图,但是当应用未运行时,无法单击该按钮!
我的实现:
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);
}
答案 0 :(得分:0)
我通过以下方法解决了这个问题:
startForeground(id, mBuilder.build());