Android 警报管理器未触发,正在尝试发送通知
Android Alarm Manager not firing, trying to send Notification
我正在尝试设置一个一次性闹钟来唤醒我的应用程序,然后向用户发送通知。
我将闹钟设置为未来 1 分钟,但我的广播接收器从未被调用。
这就是我安排 AlarmManager
和构建 Notification
的方式:
private void scheduleNotification(Date notificationdate) {
Log.d("PSBMF", "scheduling notification at "+notificationdate);
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+AppController.statewidephone));
PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);
String notificationmessage = "Test Message";
String channelid = "Channel Id";
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getActivity(), channelid)
.setSmallIcon(R.drawable.ic_warning_black_36dp)
.setContentTitle("Notification Title")
.setContentText("Notification Content")
.setStyle(new NotificationCompat.BigTextStyle().bigText("Notification Content"))
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setDefaults(Notification.DEFAULT_ALL)
;
Notification notification = mBuilder.build();
Intent notificationIntent = new Intent(mActivity, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(mActivity, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long notificationTimeInMillis = notificationdate.getTime();
AlarmManager alarmManager = (AlarmManager)mActivity.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, notificationTimeInMillis, pendingAlarmIntent);
}
我的 NotificationPublisher.class
看起来像这样:
public class NotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
public void onReceive(Context context, Intent intent) {
Log.d("NP", "NOTIFICATION RECEIVED: "+context);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
}
}
并且我已经通过将其放入清单的 <application/>
标记中来注册了我的 NotificationPublisher/BroadcastReceiver
:
<receiver android:name=".NotificationPublisher"/>
我还缺少什么?为什么我的警报不会被触发并调用 onReceive
方法?
我想我解决了我的问题:
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, notificationTimeInMillis, pendingAlarmIntent);
应该是
alarmManager.set(AlarmManager.RTC_WAKEUP, notificationTimeInMillis, pendingAlarmIntent);
尝试动态注册您的广播
NotificationPublisher myBroadcastReceiver = new NotificationPublisher();// Register the broadcast
registerReceiver(myBroadcastReceiver, intentFilter);
并改变这个
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,notificationTimeInMillis, pendingAlarmIntent);
至
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,notificationTimeInMillis, pendingAlarmIntent);
感觉link这与broadcast receivers的变化有关。
在清单中注册接收器将不再有效基于:
Note: If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. In most cases, you can use scheduled jobs instead.
以上注释复制自 link。
希望对您有所帮助。
我正在尝试设置一个一次性闹钟来唤醒我的应用程序,然后向用户发送通知。
我将闹钟设置为未来 1 分钟,但我的广播接收器从未被调用。
这就是我安排 AlarmManager
和构建 Notification
的方式:
private void scheduleNotification(Date notificationdate) {
Log.d("PSBMF", "scheduling notification at "+notificationdate);
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+AppController.statewidephone));
PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);
String notificationmessage = "Test Message";
String channelid = "Channel Id";
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getActivity(), channelid)
.setSmallIcon(R.drawable.ic_warning_black_36dp)
.setContentTitle("Notification Title")
.setContentText("Notification Content")
.setStyle(new NotificationCompat.BigTextStyle().bigText("Notification Content"))
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setDefaults(Notification.DEFAULT_ALL)
;
Notification notification = mBuilder.build();
Intent notificationIntent = new Intent(mActivity, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(mActivity, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long notificationTimeInMillis = notificationdate.getTime();
AlarmManager alarmManager = (AlarmManager)mActivity.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, notificationTimeInMillis, pendingAlarmIntent);
}
我的 NotificationPublisher.class
看起来像这样:
public class NotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
public void onReceive(Context context, Intent intent) {
Log.d("NP", "NOTIFICATION RECEIVED: "+context);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
}
}
并且我已经通过将其放入清单的 <application/>
标记中来注册了我的 NotificationPublisher/BroadcastReceiver
:
<receiver android:name=".NotificationPublisher"/>
我还缺少什么?为什么我的警报不会被触发并调用 onReceive
方法?
我想我解决了我的问题:
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, notificationTimeInMillis, pendingAlarmIntent);
应该是
alarmManager.set(AlarmManager.RTC_WAKEUP, notificationTimeInMillis, pendingAlarmIntent);
尝试动态注册您的广播
NotificationPublisher myBroadcastReceiver = new NotificationPublisher();// Register the broadcast
registerReceiver(myBroadcastReceiver, intentFilter);
并改变这个
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,notificationTimeInMillis, pendingAlarmIntent);
至
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,notificationTimeInMillis, pendingAlarmIntent);
感觉link这与broadcast receivers的变化有关。
在清单中注册接收器将不再有效基于:
Note: If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. In most cases, you can use scheduled jobs instead.
以上注释复制自 link。
希望对您有所帮助。