Notifications not received on Android

*爱你&永不变心* 提交于 2019-12-03 14:39:40

About notifications, I can tell you the following:

After a long time of developing an app which uses a lot of Alarms from AlarmManager I discovered a lot of things about some conflictive devices. (I didn't tried JobScheduler, but in most of cases this technology will also fail).

There are some manufacturers (well known, Huawei, Xiaomi, Samsung, etc) that interfers in the life cycle of AlarmManager.

Huawei and Xiaomi

Huawei by default kills all non protected apps when locking the screen. That's it, kills all resources of the app, including alarms, boradcast receivers and services. The app is completly killed after the screen is locked so the alarms are not received and the notifications are not shown logically. To avoid this situation, Huawei provides a way to put the apps in protected mode, which means that when the screen is locked, these protected apps are not killed. Protected apps, then, still receive alarms and broadcast receivers.

Samsung

Samsung has a "feature" (unwanted feature for developers) that do the "same" than Huawei and Xiaomi devices with a little difference. Samsung doesn't kills non protected apps when locking the scree, but when the app is not opened in 3 days. After 3 days of user inactivity the app is killed like Huawei and Xiaomi, so the notifications (alarms) are not received. Samsung also provides a way to protect the apps and avoid this situation.

Conclusion

There are other manufacturers with the same behavior, but I don't know all of them. I can tell you Huawei, Xiaomi and Samsung are the most known of their.

So, first try to know if all failing devices are manufactured by these conflictive manufacturers.

Then, there are some ways to let the user know that notifications may not fire in this devices, and invite them to put your application as a protected app.

Programmatically, you can do something like that (source):

if("huawei".equalsIgnoreCase(android.os.Build.MANUFACTURER)) { 
    AlertDialog.Builder builder  = new AlertDialog.Builder(this);
    builder.setTitle(R.string.huawei_headline).setMessage(R.string.huawei_text)
            .setPositiveButton(R.string.go_to_protected, new DialogInterface.OnClickListener() {
                @Override 
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
                    startActivity(intent);
                } 
            }).create().show(); 
}

And you can do the same with other conflictive manufacturers. This is how I handle this situation in these devices, and notifications works well, in most of cases.

Hope this helps.

The problem why you do not get any notifications after a while is because of DOZE.

You have 2 options for solving your issue:

The cause of this might be Doze. It works, when the device is steady and is not used, android makes background calls together with other application to save the wake up time of phone and save battery. I am not exactly sure if this is your case, but you might look into that.

This answer might help you in doing so.

Some docs says app can handle 50 notification maximum.(not sure)

But If you try to give different notification id:

for (int i...>500)
 notificationManager.notify(i, n);

Hope this works

@Mars Estrada is right, the problem is due to the phone implementation. Huawei have a software call powergenie which can kill (in linux term) your service. So the implementation clearly do not respect the doze Mode. Somme application just inform user that "since you have a huawei, be sure to set up the power management corectly". THe same goes for Xiaomi and Samsung. AS these implementation does not follow Android SDK there is no way to manage a background application. For My application for different Huawei ( HOnor 8x, Huawei P20, P10, P9 , P9 lite ), if the application is not protected for background execution in the setting, It is even impossible to start a service using Context.startService ( or startFroegroundService when available). regards Charles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!