java - 尝试在android中安排本地通知,但通知不显示
问题描述
我正在尝试从片段中安排从当前开始 2 小时的通知,但是根本没有调用 alarmreciever 中的 onRecieve 方法...
日期片段
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
AlarmManager alarms = (AlarmManager) Objects.requireNonNull(getActivity()).getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("notif.alarm.DISPLAY_NOTIF");
PendingIntent operation = PendingIntent.getBroadcast(getContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
assert alarms != null;
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 10);
alarms.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), operation) ;
}
报警接收器
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Map<String, String> data = new HashMap<>();
data.put("call_frag","0");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(Constants.CHANNEL_ID, Constants.CHANNEL_NAME, importance);
mChannel.setDescription(Constants.CHANNEL_DESCRIPTION);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400});
assert mNotificationManager != null;
mNotificationManager.createNotificationChannel(mChannel);
}
MyNotificationManager.getInstance(context).displayNotification(data);
}
}
显现
<receiver android:name=".networks.AlarmReceiver">
<intent-filter>
<action android:name="notif.alarm.DISPLAY_NOTIF"/>
</intent-filter>
</receiver>
请帮我调试问题。
解决方案
推荐阅读
- qemu - 我的 QEMU 运行失败,但我无法从详细日志中找到原因
- angular - 赛普拉斯在单独的测试中触发 cy.wait(1000) 两次
- file - 如何使用 AppleScript 获取文件的路径?
- kotlin - 将选项列表映射到字符串列表
- go - 将 Go 调用的 GCP 客户端库限制为特定组织
- laravel - Laravel - 5.4v 和 5.8v 外键的区别
- android - 我可以在布局 xml 中将 View.Tag 设置为复杂对象吗?
- google-bigquery - Google BigQuery 中 DATE、DATETIME 和 TIMESTAMP 数据类型的数据大小是多少?
- sql - 我将如何组合第二个子查询?
- android - 如何在Android中设计适合纵向/横向模式的布局?