How to show daily offline notifications in Android 10?

后端 未结 2 1774
慢半拍i
慢半拍i 2020-12-04 02:37

I want to send offline notifications to users daily.Tried many things like: Alarm manager,Work manager but no luck.The notifications are not triggered or they just trigger a

相关标签:
2条回答
  • 2020-12-04 03:07

    I can see one possible issue, and it's possible to do this without the workmanager (assuming the device has a healthy connection at the time the notification runs).

    Instead of doing your network in the receiver itself, I suggest starting a service (foreground service if above Android 8.0), and doing your work there. This is because android's time limit for a receiver is much lower than a service/foreground service. So to me, it sounds plausible that your receiver is killed before the network request completes, and so no notification is shown.

    You can show the notification in the service, and also schedule the next alarm since setExactAndAllowWhileIdle isn't a repetitive alarm on it's own. So in your receiver, something like:

    @Override
    public void onReceive(Context context, Intent intent) {
            Intent service = new Intent(context, BirthdayNotifyService.class);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(service);
            } else {
                context.startService(service);
            }
    }
    

    Then a possible service class:

    public class BirthdayNotifyService extends IntentService {
    
        public BirthdayNotifyService() {
            super("BirthdayNotifyService");
        }
    
        @Override
        public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
            super.onStartCommand(intent, flags, startId);
            return START_STICKY;
        }
    
        @Override
        protected void onHandleIntent(@Nullable Intent intent) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //Build a simple notification to show if a foreground service is neccesary
                Notification noti = notiBuilder.build();
                startForeground(1, noti);
            }
    
            //Do your network request work here. After completed, show your birthday notification and stop the foreground service.
        }
    }
    

    To stop the service, right AFTER you display your notification for birthdays, use the following:

    stopForeground(true);
    stopSelf();
    

    UPDATE: I used this method on my phone (Xiaomi Redmi Note 8 plus), it worked a few times but then stopped working. It works perfectly fine on stock android, but not on all devices. Therefore I would still say this is not a reliable solution to send notification on specific times.

    0 讨论(0)
  • 2020-12-04 03:13

    It seems like you are doing everything right, and as you said in the comments that it's working on nexus S, I assume that you also declared the receiver in the manifest.

    So after week of having the same problem here is the result:

    In some devices with custom OS like Xiaomi, when you swipe the app away from the list of recents, the OS considers it as a force stop and therefor all services and other stuff will be killed. Based on Issue Tracker there is no way around it at the moment to solve this issue. They responded that they are working with OEMs to resolve this issue.

    But based on my own experience, if you setup your notification using work manager, it will be delayed, but you will eventually receive it (at least most of the time).

    But if you want the timing to be exact, there is no way to proceed at the moment.

    The only solution at the moment is to give some permission manully at the moment. Please visit dontkillmyapp for more information regarding this manual settings.

    0 讨论(0)
提交回复
热议问题