[英]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"/>
我还想念什么? 为什么我的Alarm不会被触发并调用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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.