首页 > 解决方案 > FCM push notification depending on app state

问题描述

I am testing the same app on two different ANDROID devices.

On has version 8.1.0 (Moto G5s), the other has version 7.0 (Galaxy A5).

I am working with Firebase CloudMessaging.

Scenario 1: push sent from Firebase console, app is closed, both devices receive the notification and show the notification on status bar

Scenario 2: push sent from Firebase console, app is started but in background, both devices receive the notification and show the notification on status bar

Scenario 3: push sent from Firebase console, app is started and in foreground, both devices receive the notification but only the 7.1 device shows the notification on status bar.

This is the code in AndroidManifest:

<service
    android:name=".activity.MyFirebaseService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

And this is MyFirebaseService file:

public class MyFirebaseService extends FirebaseMessagingService {


    @Override
    public void onNewToken(String s) {
        FirebaseInstanceId.getInstance().getInstanceId()
                .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                    @Override
                    public void onComplete(@NonNull Task<InstanceIdResult> task) {
                        if (!task.isSuccessful()) {
                            Log.w( "getInstanceId failed", task.getException());
                            return;
                        }

                        // Get new Instance ID token
                        String token = task.getResult().getToken();
                        Log.e("My Token",token);
                    }
                });
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        super.onMessageReceived(remoteMessage);
        Log.d("push recibida","push reecibida "+remoteMessage);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody())
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setStyle(new NotificationCompat.BigTextStyle())
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true);

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

        notificationManager.notify(0, notificationBuilder.build());
    }
}

I would like to know an explanation of the 3 scenarios and then I would appreciate a way to decide to show or not to show the notification icon depending on the state of the app: 1. not launched 2. launched but in background 3. launched and in foreground

标签: androidfirebasepush-notificationfirebase-cloud-messaging

解决方案


推荐阅读