FCM push notification not working on some devices

Viewed 1691

We have created an chat app with notifications using FCM my code is correct my device is getting the push notification data as well but some Chinese manufactured device like vivo, oppo, one plus, xiaomi are not allowing the notification to show unless i add app in protected app list of respective manufacturer. is their any solution for this.

https://hackernoon.com/notifications-in-android-are-horribly-broken-b8dbec63f48a

https://github.community/t5/Project-Development-Help-and/Firebase-Push-Notification-not-receiving-on-some-android-devices/td-p/5489

private NotificationManagerCompat notificationManager;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Log.d("test","call");

    notificationManager = NotificationManagerCompat.from(this);
    sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}

private void sendNotification(String title, String msg) {
    Intent resultIntent = new Intent(this, ActivitySplashScreen.class);

    String channelId = getString(R.string.chc);
    String channelName = "Message Notification";

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntentWithParentStack(resultIntent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(99, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notification = new NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentTitle(title)
            .setContentText(msg)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .setContentIntent(pendingIntent)
            .build();

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationManager manager = getSystemService(NotificationManager.class);
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
        manager.createNotificationChannel(channel);
    }

    notificationManager.notify(10, notification);
}
2 Answers

I can confirm that this implementation works well with One Plus and Xiaomi devices (we have plenty of users using our app from this devices, and no crashes or issues are created from them regarding FCM Notifications).

Cannot confirm anything for Vivo or Oppo (so far, we do know we don't have users using this kind of devices).

The most important thing is that the notification feature is well implemented if regarding the app is either in foreground or background. If someone wants to do a zoom on this matter, this article explains it on a easy way.

Now, the code I use for FCM implementation in Android:

//MyMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {

  public static final String TAG = "Firebase Msg";

  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> data = remoteMessage.getData();
    final LocalNotification localNotification = new LocalNotification();
    //data --> entityId - entityType - pushNotificationType
    // Check if message contains a data payload.
    if (data.size() > 0) {
        localNotification.setEntityId(data.get("entityId"));
        localNotification.setEntityType(data.get("entityType"));
        localNotification.setPushNotificationType(data.get("pushNotificationType"));
        localNotification.setTitle(data.get("title"));
        localNotification.setBody(data.get("body"));
        localNotification.setIcon(data.get("icon"));
        localNotification.setDate(new Date().getTime());
        localNotification.setRead(false);
    }

    if (localNotification.getEntityId() != null) {
        LocalNotification notificationRetrieved = FirebaseNotificationsHelper.insertLocalNotification(localNotification);
        FirebaseNotificationsHelper.createNotificationInStatus(notificationRetrieved);
    }
  }
}

//Create notification status

static void createNotificationInStatus(LocalNotification localNotification) {

    String notificationChannelId = App.getContext().getString(R.string.default_notification_channel_id);
    String notificationChannelName = App.getContext().getString(R.string.default_notification_channel_name);
    String notificationChannelDescription = App.getContext().getString(R.string.default_notification_channel_description);

    NotificationCompat.Builder notificationBuilder;
    NotificationCompat.Builder notificationBuilderPublicVersion;

    notificationBuilder = new NotificationCompat.Builder(App.getContext(), notificationChannelId);
    notificationBuilderPublicVersion = new NotificationCompat.Builder(App.getContext(), notificationChannelId);

    notificationBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE | NotificationCompat.DEFAULT_LIGHTS | NotificationCompat.DEFAULT_SOUND)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_stat_push_notif)
            .setTicker(localNotification.getTitle())
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentTitle(localNotification.getTitle())
            .setContentText(localNotification.getBody())
            .setStyle(new NotificationCompat.BigTextStyle().bigText(localNotification.getBody()))
            .setPublicVersion(notificationBuilderPublicVersion.setSmallIcon(R.drawable.ic_stat_push_notif).setContentTitle(localNotification.getTitle()).
                    setWhen(System.currentTimeMillis()).setContentText(localNotification.getBody()).build())
            .setGroup(NOTIFICATION_GROUP)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
            .setAutoCancel(true);

    int notificationId = SharedPreferencesUtils.getInstance(App.getContext()).getIntValue(PREF_KEY_NOTIFICATION_ID, 0);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager notificationManager = (NotificationManager) App.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId, notificationChannelName, NotificationManager.IMPORTANCE_HIGH);
        // Configure the notification channel.
        notificationChannel.setDescription(notificationChannelDescription);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
        notificationChannel.enableVibration(true);
        notificationChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        notificationChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(notificationChannel);
        notificationManager.notify(notificationId, notificationBuilder.build());
    } else {
        /* Kitkat and previous versions don't show notifications using NotificationManagerCompat */
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
            NotificationManager notificationManager = (NotificationManager) App.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(notificationId, notificationBuilder.build());
        } else {
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(App.getContext());
            notificationManager.notify(notificationId, notificationBuilder.build());
        }
    }
    notificationId++;
    SharedPreferencesUtils.getInstance(App.getContext()).setValue(PREF_KEY_NOTIFICATION_ID, notificationId);
}

I donot think that there is a general solution for this since your app is based on google-FCM that means that the android device regularly comunicates with google-internet-services.

As far as i know google-FCM is blocked (= not reachable) in china

Related