FCM push notifications do not arrive with the app closed

1

When the app is open or behind the scenes, all right, notifications arrive, but when the app is closed, it only arrives on some cell phones.

For example, in a Sony it arrives very well when the app is closed, but in a Huawei Y6 it does not arrive if the app is closed.

Code:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    private static final String TAG = "MyFirebaseIIDService";

    @Override
    public void onTokenRefresh() {

        //Getting registration token
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

       // SharedPreferences.getInstance(getApplicationContext()).setString(SPKeys.Keys.API_TOKEN,refreshedToken);
        //Displaying token on logcat
        Log.d(TAG, "Refreshed token: " + refreshedToken);

    }
}
public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void handleIntent(Intent intent) {
        super.handleIntent(intent);
        try {
            if (intent.getExtras() != null) {
                Log.i(TAG, "handleIntent: " + intent.getExtras().toString());

                if (!isAppIsInBackground(getApplicationContext())) {
                    String title = intent.getExtras().get("gcm.notification.title").toString();
                    String body = intent.getExtras().get("gcm.notification.body").toString();
                    String url = intent.getExtras().get("URL").toString();


                    sendNotification(title, body, url);
                } else {
                    Utility.PUSH_NOTIFICATIONMESSAGE = intent.getExtras().get("URL").toString();
                    Log.d(TAG, "Refreshed token: en handle intent " + Utility.PUSH_NOTIFICATIONMESSAGE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "Refreshed token: en catch " + Utility.PUSH_NOTIFICATIONMESSAGE);
        }
    }


    private void sendNotification(String messageBody, String tittle, String urlIMG) {
        PendingIntent pendingIntent = null;
        if (urlIMG == null && urlIMG.equalsIgnoreCase("")) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            pendingIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_ONE_SHOT);
        } else {
            Uri uri = Uri.parse(urlIMG);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            pendingIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_ONE_SHOT);
        }

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        final  NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        /*notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher));*/
        notificationBuilder.setSmallIcon(R.drawable.ic_notifications_black_24dp);
        notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher));

        notificationBuilder.setContentTitle(tittle);
        notificationBuilder.setContentText(messageBody);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSound(defaultSoundUri);
        notificationBuilder.setContentIntent(pendingIntent);

    }


    public static boolean isAppIsInBackground(Context context) {
        boolean isInBackground = true;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
            for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
                if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    for (String activeProcess : processInfo.pkgList) {
                        if (activeProcess.equals(context.getPackageName())) {
                            isInBackground = false;
                        }
                    }
                }
            }
        } else {
            List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
            ComponentName componentInfo = taskInfo.get(0).topActivity;
            if (componentInfo.getPackageName().equals(context.getPackageName())) {
                isInBackground = false;
            }
        }

        return isInBackground;
    }
}

and in my Manifest:

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@android:color/transparent" />

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

    <service android:name=".notification.MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
    
asked by Hopi Programando 13.12.2017 в 00:10
source

2 answers

0

It is important to clarify that the Push Notifications of FCM should arrive even if the application is closed.

If your implementation is correct and the reading of the Payload data should be correct, since the client application is responsible for processing and generating the message from the data .

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // ...

    // TODO(developer): Handle FCM messages here.
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

        if (/* Check if data needs to be processed by long running job */ true) {
            // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
            scheduleJob();
        } else {
            // Handle message within 10 seconds
            handleNow();
        }

    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

The cases in which users report not receiving notifications are:

They do not have notification notifications enabled.

On the closed / off screen you have enabled not to show notifications.

    
answered by 13.12.2017 в 00:25
0

I have a Huawei Y6 II and I also went through this. The Huawei system by default does not allow applications to send notifications, for this you must enable them from Gestor del teléfono > Centro de notificaciones and there you will select your app and enable notifications.

There is supposedly a method to enable them by code ( link ) but I have not tried it.

    
answered by 16.12.2017 в 08:34