0

Did some research on making FCM notification work some devices when the application is killed by the user(swiped away by the user from the app tray) or killed by the system OS but did not find any solution would appreciate if you can provide with solution or tell me what i am dong wrong. The notification is a data payload

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String payload = "",notification_message = "", channelId ="", channelName="AAA", channelDescription="";
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        String title = null, body = null;
        if(remoteMessage.getNotification() != null) {
            Log.d("REMOTE_DATA",remoteMessage.getNotification().getBody());
            title = remoteMessage.getNotification().getTitle();
            body = remoteMessage.getNotification().getBody();
        }

        if(remoteMessage.getData() != null) {
            if(remoteMessage.getData().get("title") != null) 
               title = remoteMessage.getData().get("title");

            if(remoteMessage.getData().get("message") != null) 
               body = remoteMessage.getData().get("message");

            if(remoteMessage.getData().get("channelName") != null) 
               channelName = remoteMessage.getData().get("channelName");
        }
    }

    //Setting up Notification channels for android O and above
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        Log.e(TAG, "onMessageReceived: => " + android.os.Build.VERSION.SDK_INT);

        if(notificationChannels.containsKey(channelName)) {
            JSONObject channelData = (JSONObject) notificationChannels.get(channelName);
            try {
                channelId = channelData.getString("channelId");
                channelName = channelData.getString("channelName");
                channelDescription = channelData.getString("channelDescription");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId);
            if (notificationChannel == null)
                setupChannel(channelId, channelName, channelDescription);
        }
    }

    Log.d(TAG, "Notification Message Body: " + title);
    Log.d(TAG, "Notification Message Body: " + body);
    if(title != null && body != null) {
        apptivity_variable.createNotification(channelId,title, body);
        db.insertNotification(title, body, remoteMessage.getData().toString());
    }
}

3 Answers3

1

Try registering another service to identify the app in the background to trigger the service

public class FirebaseBackgroundService extends BroadcastReceiver{

  private static final String TAG = "FirebaseService";

  @Override
  public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "I'm in!!!");

    if (intent.getExtras() != null) {
      for (String key : intent.getExtras().keySet()) {
        Object value = intent.getExtras().get(key);
        Log.e("FirebaseDataReceiver", "Key: " + key + " Value: " + value);
        if(key.equalsIgnoreCase("gcm.notification.body") && value != null) {
          Bundle bundle = new Bundle();
          Intent backgroundIntent = new Intent(context, BackgroundSyncJobService.class);
          bundle.putString("push_message", value + "");
          backgroundIntent.putExtras(bundle);
          context.startService(backgroundIntent);
        }
      }
    }
  }
}

Also, don't forget to register it in your manifest.xml

<receiver android:exported="true" android:name=".FirebaseBackgroundService" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </receiver>
Samuel Moshie
  • 570
  • 1
  • 5
  • 20
0

I had same issue and after searching and working around, I have found that user need to enable autostart and battery optimization permission manually for more details refer this link

This issue is generally coming most of the Chinese phones to optimize battery etc.

this link will solve your problem

Amin Pinjari
  • 2,129
  • 3
  • 25
  • 53
0

There are two different types of push messages priorities in FCM: normal priority and high priority. Only high priority pushes will wake your application up when it's in background and is the subject of Doze Mode restrictions. You can read more about it in this article

To make priority of your push message high, you have to add this section to the body of your push message:

"android":{"priority":"high"}

More information about push messages is available here

Stanislav Shamilov
  • 1,746
  • 11
  • 20