0

I made apps that need to get a notification though the apps are closed. I'm using fcm to get the notification. But when the apps are removed from the recent task, I stop getting a notification. I had seen other article but I still can't get the answer to my problem.

I'm using firebase:

'com.google.firebase:firebase-messaging:11.0.2'

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e(TAG, "From: " + remoteMessage.getFrom());
    Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
    Log.e(TAG," remote :" +remoteMessage.toString() );
    try {
        JSONObject json = new JSONObject(remoteMessage.getData());
        String title = json.get("title").toString();
        String message = json.get("message").toString();
        createNottification(title, message);
    }
    catch (Exception ex){}

    if(remoteMessage.getNotification() != null)
        Log.d(TAG, "Message Notification Body: "+remoteMessage.getNotification().getBody());
}

private void createNottification(String title,String message)
{
    Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);;
    Notification notification = new NotificationCompat.Builder(this)
            .setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.mipmap.pmi_launcher)
            .setSound(notificationSoundURI                )
            .build();

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
    notificationManager.notify(Config.NOTIFICATION_ID, notification);

}
}

Manifest.xml

 <!-- Firebase Notifications -->
    <service android:name=".services.MyFirebaseMessagingService" android:stopWithTask="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <service android:name=".services.MyFirebaseInstanceIDService"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Nicky Apriliani
  • 321
  • 4
  • 25
  • facing the same issue for specific devices (Samsung s8, one plus 3) – akhilesh0707 Jul 22 '17 at 07:37
  • 1
    Possible duplicate of [Android app not receiving Firebase Notification when app is stopped from multi-task tray](https://stackoverflow.com/questions/39504805/android-app-not-receiving-firebase-notification-when-app-is-stopped-from-multi-t) – AL. Jul 25 '17 at 03:35

1 Answers1

0

first pass PendingIntent and also pass unique notification ID for generate bunch or stack of notification. also set Flags for notification Intent.so just do this in this way. Hope it helps to you and also other users.

 private void sendNotification(String message,String title) {
    int id = (int) System.currentTimeMillis();
    Intent notificationIntent = new Intent(this, SplashActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(title)
            .setContentText(message)
            //.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image).setSummaryText(message))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    } else {
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    }

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(id, notificationBuilder.build());
}
Dharmishtha
  • 1,313
  • 10
  • 21
  • This doesn't help at all. Issue is because of some devices don't allow to restart app services in background. User has to white list the app in settings. For example in RedMi phones this setting may be found under Settings > Security – Kaustuv Aug 22 '17 at 09:59