【发布时间】:2019-08-12 11:52:53
【问题描述】:
在我的Service 的onCreate 方法中,我通过执行以下操作来创建通知:
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);
}
上面Intent中的alarmReceiver在我的manifest中注册了如下图(我看到this问题后做了以下操作):
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="alarmReceiver" />
</intent-filter>
</receiver>
这是我的AlarmReceiver 课程:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("onReceive - ","was called");
}
}
显示通知以及按钮,但是当我按下按钮时没有任何反应。
我不确定我做错了什么。任何帮助将不胜感激。
【问题讨论】:
-
可能是接收者的
onReceive()被称为检查日志。 -
同时用完整的包名注册 .AlarmReceiver
-
@KalpeshKulye 我检查了日志,没有被调用。
-
@KalpeshKulye stackoverflow.com/q/35647821/8199772
标签: java android android-service android-notifications