首页 > 解决方案 > 尝试在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>

请帮我调试问题。

标签: javaandroidpush-notificationscheduling

解决方案


推荐阅读