Gopal
Gopal

Reputation: 570

Did not get Notification in Android mobile when Application removed from the Recent Used Apps

I am using firebase cloud messaging for send notification to my application.I get notification when my application in foreground and also switch to another app (my application running in background not closed). But when I was removing from recent used apps.I didn't get notification. I get the Token Id also. Problem is didn't get the notification when app is removed from the latest used apps.

Android Manifest file: Inside application

  <service
        android:name="com.mysite.android.abc.service.MyFirebaseMessagingService"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
        >
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>

        </intent-filter>
    </service>
    <!-- [END firebase_service] -->
    <!-- [START firebase_iid_service] -->
    <service
        android:name="com.mysite.android.abc.service.MyFirebaseInstanceIDService"

        >
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
    <!-- [END firebase_iid_service] -->
    <receiver
        android:name=".service.FirebaseDataReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </receiver>

MyFirebaseInstanceIDService:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

@Override
public void onTokenRefresh() {
    super.onTokenRefresh();
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    // Notify UI that registration has completed, so the progress indicator can be hidden.
    Intent registrationComplete = new Intent( Config.REGISTRATION_COMPLETE);
    registrationComplete.putExtra("token", refreshedToken);
    LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);

}
}

MyFirebaseMessagingService:

 public class MyFirebaseMessagingService extends FirebaseMessagingService {
private NotificationUtils notificationUtils;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived( remoteMessage );
    if(remoteMessage==null){
        return;
    }
    if (remoteMessage.getNotification()!=null) {
        handleNotification( remoteMessage.getNotification().getBody() );

    }
    if(remoteMessage.getData().size()>0){

        try {
            JSONObject jsonObject=new JSONObject( remoteMessage.getData().toString() );
            handleDataMessage( jsonObject );


        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
private void handleNotification(String message){
    if(!NotificationUtils.isAppIsInBackground( getApplicationContext() )){
        Intent pushNotification=new Intent( Config.PUSH_NOTIFICATION );
        pushNotification.putExtra( "message",message );
        LocalBroadcastManager.getInstance( this ).sendBroadcast( pushNotification );
        // play notification sound
        /*NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
        notificationUtils.playNotificationSound();*/
    }else{

    }
}
private void handleDataMessage(JSONObject jsonObject){
    Log.e("GGGGG mm",jsonObject.toString());
    try {
        JSONObject data=jsonObject.getJSONObject( "data" );
        String title=data.getString( "title" );
        String message=data.getString( "message" );
        JSONObject payload = data.getJSONObject("payload");
        if(!NotificationUtils.isAppIsInBackground( getApplicationContext() )){
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

            // play notification sound
            /*NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
            notificationUtils.playNotificationSound();*/
        } else {
            // app is in background, show the notification in notification tray
            Intent resultIntent = new Intent(getApplicationContext(), Dashboard.class);
            resultIntent.putExtra("message", message);

            // check for image attachment
            if (TextUtils.isEmpty(imageUrl)) {

                showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
            } else {
                // image is present, show notification with image
                showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, imageUrl);
            }
        }


    } catch (JSONException e) {
        e.printStackTrace();
    }

}

/**
 * Showing notification with text only
 */
private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {
    notificationUtils = new NotificationUtils(context);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
}

/**
 * Showing notification with text and image
 */
private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) {
    notificationUtils = new NotificationUtils(context);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
}
}

Upvotes: 2

Views: 473

Answers (1)

Jay Sidri
Jay Sidri

Reputation: 6406

What you are asking is not supported on FCM (this used to work with GCM which is now deprecated)

https://developer.android.com/about/versions/android-3.1#launchcontrols:

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. A background service or application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications.

Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications).

The broadcast FCM triggers has FLAG_EXCLUDE_STOPPED_PACKAGES set to TRUE which effectively will not wake up your app if it's force stopped by the user (as you did).

Someone requested that firebase allow the user to control this flag - but this was shot down by the dev team

Upvotes: 1

Related Questions