Search code examples
androidfirebase-cloud-messagingbackground-processsleep-mode

start an activity from background after a while


I'm developing an app that needs to start an activity when a FCM message is received. Everything works fine but as long as I do not use the phone for a while and the phone goes to sleeping mode and the activity does not start. How can I prevent my app go sleeping and force it stay on backgound all the time?

I actually want to run a calling activity like what WhatsApp uses. I have added the following permissions to the manifest file:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.webkit.PermissionRequest" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

 <activity
        android:name=".RingingActivity"
        android:excludeFromRecents="true"
        android:noHistory="true"
        android:screenOrientation="sensorPortrait"
        android:showOnLockScreen="true"
        android:showWhenLocked="true"
        android:theme="@style/AppTheme.NoActionBar"
        android:turnScreenOn="true" />

And I added the following code to my RingingActivity before setContentView

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        setShowWhenLocked(true);
        setTurnScreenOn(true);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        if (keyguardManager != null)
            keyguardManager.requestDismissKeyguard(RingingActivity.this, null);
    } else {
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                        | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                        | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

After receiving the FCM message, I start the activity as follows

public void onMessageReceived(RemoteMessage message){
\\...
Intent ringing_intent = new Intent(getApplicationContext(), RingingActivity.class);
ringing_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
startActivity(ringing_intent);
}

I also started the activity by using AlarmManager

am =(AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        pendingIntent = PendingIntent.getActivity(this,
                12345, ringing_intent, PendingIntent.FLAG_CANCEL_CURRENT);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 100, pendingIntent);

In both methods, the activity comes over the lock and starts running, but after a while it does not work anymore.

p.n. I Also removed my app manually from the Sleeping apps in phone settings. but not useful.


Solution

  • I solved my problem by setting priority : "high"on FCM message