关闭应用程序后,Android重复通知无法正常工作

编程入门 行业动态 更新时间:2024-10-10 18:23:00
本文介绍了关闭应用程序后,Android重复通知无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想每天在特定时间发送通知.该代码在打开应用程序时有效.但是,当它关闭并删除时,通知不会显示.我已经为此使用了广播接收器和服务.代码如下.任何人都可以帮助解决此问题.

I want to send notification everyday on a particular time. The code is working when the app is opened. But when it closed and remove, the notifications are not showing. I have used broadcast receiver and service to this. The code is given below. Can anyone help to clear this issue.

清单文件

<receiver android:name=".MyReceiver" android:enabled="true" android:exported="true" /> <service android:name=".MyService" android:enabled="true" android:exported="true" />

MyReceiver.java

public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { intent = new Intent(context, MyService.class); context.startService(intent); }}

MyService.java

public class MyService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { createNotification(); return Service.START_STICKY; } private static final String NOTIFICATION_CHANNEL_ID = "Channel01"; private void createNotification() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); String name = preferences.getString("name", "User"); name = name.split(" ")[0]; String namee = "Remainder"; String description = "Remainder to update Wallet"; int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, namee, importance); notificationChannel.setDescription(description); Intent notifyIntent = new Intent(getApplicationContext(), MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, notifyIntent, 0); Notification notification = new Notification.Builder(getApplicationContext()) .setContentTitle("Remainder") .setContentText("Hey " + name + ", Let's update your wallet") .setSmallIcon(R.drawable.wallet) .setChannelId(NOTIFICATION_CHANNEL_ID) .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.wallet_new)) .setContentIntent(pendingIntent) .build(); NotificationManager notificationManager = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(notificationChannel); // Issue the notification. notificationManager.notify(1 , notification); } }}

Activity.java

Intent notifyIntent = new Intent(getApplicationContext(), MyReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, notifyIntent, 0); alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, timeMilli, timeInterval, pendingIntent);

推荐答案

您不应使用应用程序的任何部分来执行此操作.服务,JobScheduler或Work Manager迟早会被系统杀死,以防止电池耗尽.

You shouldn't use any part of application to do that. Services, JobScheduler, or Work Manager sooner or later will be killed by the system to prevent battery drain.

我认为发送重复通知的最佳方法是使用由外部cron作业(例如,firebase函数上的php)触发的firebase云消息传递.

In my opinion the best way to send repeated notifications is to use firebase cloud messaging triggered with external cron job (e.g. php on firebase functions).

还要确保将通知发送到系统托盘而不是应用程序.为此,请使用FCM DataMessages.数据消息传递到系统托盘,并且始终显示-即使服务未运行.

Also make sure to deliver the notification to the system tray not to the application. To do that use FCM DataMessages. Data messages are delivered to system tray and are always display - even if service is not running.

更多推荐

关闭应用程序后,Android重复通知无法正常工作

本文发布于:2023-11-27 10:03:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637620.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   应用程序   通知   工作   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!