android - Android 警报管理器未触发,尝试发送通知
问题描述
我正在尝试设置一个一次性警报来唤醒我的应用程序,然后向用户发送通知。
我将在未来设置 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);
}
}
我已经NotificationPublisher/BroadcastReceiver
通过将其放入<application/>
清单的标签中来注册我的:
<receiver android:name=".NotificationPublisher"/>
我还缺少什么?为什么我的警报不会被触发并调用该onReceive
方法?
解决方案
推荐阅读
- twilio - Kamailio 转移标头
- java - 如何在 Netbeans 中为多个 JTextField 创建一个 keyTyped 方法
- typo3 - TYPO3 v8.7。- 语言切换无法正常工作以获取自己扩展的详细记录
- python - 试图提取日期列和收盘价(SPY)
- retrofit - 即使在活动更改后如何让 RxJava Observables 工作?
- android - 从 ImageView 顶部限制到 50%
- android - LinearLayout 的问题以及将可见性从 GONE 更改为 VISIBLE
- typescript - remove null or undefined from properties of a type
- python - 熊猫:在每列上应用 lambda 时使用行名
- android - Angular Material 单选按钮在 Android 4.4 上不起作用