在onCreate
的{{1}}方法中,我通过执行以下操作创建了一个通知:
Service
上方String channelId = "001";
String channelName = "myChannel";
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE);
channel.setLightColor(Color.BLUE);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (manager != null) {
manager.createNotificationChannel(channel);
Notification notification;
Intent myIntent = new Intent("alarmReceiver");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);
Notification.Action action = new Notification.Action.Builder(
Icon.createWithResource(this, R.drawable.ic_stop_black_24dp),
"action string",
pendingIntent).build();
//Modify notification badge
notification = new Notification.Builder(getApplicationContext(), channelId).setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setCategory(Notification.CATEGORY_SERVICE)
.addAction(action)
.build();
startForeground(101, notification);
}
中的alarmReceiver
已注册到我的清单中,如下所示(我在看到this问题之后做了以下操作):
Intent
这是我的<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="alarmReceiver" />
</intent-filter>
</receiver>
课:
AlarmReceiver
显示了通知以及按钮,但是当我按下按钮时,什么也没有发生。
我不确定自己在做什么错。任何帮助将不胜感激。
答案 0 :(得分:0)
将PendingIntent设置为前台服务,并可能添加一个标志:
FLAG_UPDATE_CURRENT
PendingIntent pendingIntent = PendingIntent.getForegroundService(this, 0, myIntent, FLAG_UPDATE_CURRENT);
您的广播是Implicit
,因此,如果要使用getBroadcast()
,最好通过向接收者的Explicitly
提供意图,componentName
声明接收者
例如:
intent.setComponent(new ComponentName("packagename","fully qualified class name"));
您是否已在清单中声明了该服务?