繁体   English   中英

Android Studio 中的通知未按时发布

[英]Notification in Android Studio Not Coming on Time

我目前正在使用 java 在 android 工作室上开发一个应用程序,我希望用户能够从他们创建的日历事件中接收通知。 但是,我的通知没有按时发送,因为它们要么滞后要么只是没有显示。

这是我设置通知的警报接收器的编码:

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String event = intent.getStringExtra("event");
        String time = intent.getStringExtra("time");
        int notId = intent.getIntExtra("id", 0);
        Intent activityIntent = new Intent(context, CalendarActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_ONE_SHOT);

        String channelId = "channel_id";
        CharSequence name = "channel_name";
        String description = "description";
        NotificationChannel channel = new NotificationChannel(channelId, name, NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription(description);
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);

        Notification notification = new NotificationCompat.Builder(context, channelId)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentTitle(event)
                .setContentText(time)
                .setDeleteIntent(pendingIntent)
                .setGroup("Group_calendar_view")
                .build();
        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
        notificationManagerCompat.notify(notId,notification);
    }
}

这是我设置警报的 CustomCalendarView 活动:

private void setAlarm(Calendar calendar, String event, String time, int RequestCode){
        Intent intent = new Intent(context.getApplicationContext(), AlarmReceiver.class);
        intent.putExtra("event",event);
        intent.putExtra("time",time);
        intent.putExtra("id",RequestCode);
        @SuppressLint("UnspecifiedImmutableFlag") PendingIntent pendingIntent = PendingIntent.getBroadcast(context,RequestCode,intent,PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmManager = (AlarmManager)context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() ,pendingIntent);
    }

例如:当我将闹钟设置为上午 10:20 时,通知要么没有弹出,要么很晚才弹出,比如上午 10:22。 请帮忙。 如果我需要提供更多信息,请告诉我。

您应该使用精确的警报按时发出警报,

使用 alarmManager.setExact() 而不是 alarmManager.set()。

setExact() 方法类似于 set(),但不允许操作系统调整交付时间。 警报将尽可能接近请求的触发时间。

把 setExact() 代替 set

alarmManager.setExact(...);

但是随着设备和界面更新的不同,应用程序的工作性能因设备而异,在这种情况下,您必须settings in your application such as Disable Software Battery Optimizations, make the application run in the background even after closing, give Permissions Autostart, turn on all notifications and display them etc.最后,在ActivityonStop () 事件中调用函数。

是,对的。 您应该要求用户允许电池优化,并且在 Android 12 或更高版本中,您还应该要求用户启用警报和提醒权限。

但是,当您的应用想要准确地按时发出警报时,您必须使用准确的警报。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM