40

I am working on an application where I am using Google Push Notification. Application receives notification when it is running in Xiaomi phone otherwise when it's killed it does not receive notification.

If we want to receive notification if application is killed then we need to allow auto restart app manually from security app of xiaomi. I want any trick to do this programmatically without asking user. Is there any way to do this ?

enter image description here

http://en.miui.com/thread-33826-1-1.html

Community
  • 1
  • 1
N Sharma
  • 33,489
  • 95
  • 256
  • 444

4 Answers4

13

There are five settings that needs to be done manually in case of xiaomi to properly run any application. I have done a lot of research on this and there's no way to fix these settings programmatically. These are the settings:

  1. Auto Start -> ON (Toggle and restart your app)
  2. MIUI Optimization under Developer Options -> OFF
  3. Memory Optimization under Developer Options -> LOW/OFF
  4. No restrictions on background activities under Battery & Performance Settings
  5. Battery Saver -> OFF

There are many other devices in which the manual settings needs to be done in order for the app to work as expected e.g. Lenovo, some Micromax devices. Companies impose these kind on restrictions on background activities to improve the overall battery life. Some apps like facebook and whatsapp work correctly as these might have been included as system apps.

Aman Kaushik
  • 360
  • 2
  • 10
  • thanks, i just tested on my xiaomi with MIUI global 9.1 and now my Intent Services works! – Saul_programa Jun 01 '18 at 05:40
  • This has been ***whole-sale*** [plagiarised](https://en.wiktionary.org/wiki/plagiarize#Verb) from *[WCC2 Guides](https://www.cricketbuddies.com/cricket/world-cricket-championship-2/guides/wcc2-guides)*. Or in other words, there isn't a ***single*** word of the OP's own. – Peter Mortensen Aug 31 '23 at 11:39
6

After MIUI 6 & 7:

MIUI power saving mode is default set to "Standard" (Background access to the location services and the network will be restricted)

Where to set:

Settings -> Additional settings -> Battery & performance -> Manage apps battery usage -> Power Saving Modes -> Set to Off (MIUI won't restrict background activities)

Tony Yeh
  • 71
  • 1
  • 5
3

As for my understanding once you clear apps or clear memory in Recent Apps menu, xiaomi (or MIUI rom) will force close all the services and memory related to that app similar to user going to settings and force stopping app ,

This Link talks about the same issue, hence all the Broadcast receivers and Services will be ended unless started by the user again, so the notification wont be received,

However you can try just enabling auto-start for your app permissions in settings and If it still doesn't work try creating service that restarts by itself and enable auto-start in the settings, AutoStart is very important in MIUI, untill its enabled all the notification or app activity will be force closed and will never start

Ujju
  • 2,723
  • 3
  • 25
  • 35
  • How to create a service that restarts by itself? – Manikanta Nov 05 '15 at 14:15
  • 1
    You can do It by many ways , first try the START_STICKY ,so it tries to recreate service if its ended by system, or Just listen to onDestroy of service and try to start again – Ujju Nov 06 '15 at 10:29
  • Other apps like Facebook, whatsapp etc, they send notifications even when my app is not in ROM so can you please tell me how these apps solve this issue in MI phones. – Sudhanshu Gaur Jul 26 '16 at 18:55
  • @SudhanshuGaur No, it wont run any background services if its not enabled in 'Autostart', by default all the newly installed apps are in disabled mode, However few popular apps like FB and WhatsApp are Enabled by dafault when you install , but you can still disable them manualy and test fr urself, they wont receive any more messages – Ujju Jul 27 '16 at 19:49
  • 1
    Yea i already know that, i wanted to ask what code does FB or Whatsapp have wrote so that their apps are installed by default with 'autostart' option. – Sudhanshu Gaur Jul 27 '16 at 20:43
  • @SudhanshuGaur Either that or ROM has already whitelisted some very common apps by itself without FB needing to write any code, Since MIUI does not provide any SDK of its own to control its custom settings I would go with latter – Ujju Jul 28 '16 at 11:05
  • Any Update on this ? – Haroon Dec 27 '16 at 19:18
  • Any one found solution? – Bhavin Patel Apr 29 '17 at 10:18
  • How to find in which phone need to do auto-start code? Means I know In **MI device** it is required for auto-start But don't know about others . – Bhavin Patel Apr 29 '17 at 10:36
0

I faced a similar issue and fixed it by adding a BOOT_COMPLETED receiver to my app.

Add following to manifest :

<receiver
    android:name=".receivers.BootReceiver"
    android:enabled="true">
    <intent-filter>
       <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

Then create your BootReceiver class

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {

            Intent startServiceIntent = new Intent(context, FBTokenService.class);
            context.startService(startServiceIntent);

            Intent notificationServiceIntent = new Intent(context, FBNotificationService.class);
            context.startService(notificationServiceIntent);
        }
    }
}

It should work with this.

Nikhil Gupta
  • 881
  • 6
  • 14