Firebase уведомления не приходят на моем устройстве

Я реализовал Firebase, как указано в документации Google. Отправляю сообщение с консоли Firebase. Но уведомления не приходят на моем устройстве. Вы можете мне помочь? Заранее спасибо.

В моем ‘ AndroidManifest.XML

<uses-permission android_name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android_name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android_name="android.permission.WAKE_LOCK" />
<uses-permission android_name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android_name="android.permission.INTERNET" />
<uses-permission android_name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android_name="android.permission.ACCESS_FINE_LOCATION" />

<permission
    android_name="android.permission.INTERACT_ACROSS_USERS_FULL"
    android_protectionLevel="signature" />

<application
    android_allowBackup="true"
    android_icon="@mipmap/ic_launcher"
    android_label="@string/app_name"
    android_supportsRtl="true"
    android_theme="@style/AppTheme">
    <activity
        android_name=".another.IntroActivity"
        android_theme="@style/AppTheme.NoActionBar"
        android_screenOrientation="portrait">
        <intent-filter>
            <action android_name="android.intent.action.MAIN" />

            <category android_name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android_name=".firebase.MyFirebaseInstanceIdService">
        <intent-filter>
            <action android_name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
    <service android_name=".firebase.MyFirebaseMessagingService">
        <intent-filter>
            <action android_name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
</application>

В MyFirebaseInstanceIdService.java

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
private static final String TAG = "FBIIS";

@Override
public void onTokenRefresh() {
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

}
}

В MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private String TAG = "MFMS";

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

    Map<String, String> newMsg = remoteMessage.getData();

    Set<String> keys = newMsg.keySet();
    Iterator<String> iterator = keys.iterator();

    while (iterator.hasNext()) {
        String key = iterator.next();

        Log.e(TAG, "key = " + key + " value = " + newMsg.get(key));
    }


    Log.d(TAG, "From: " + remoteMessage.getFrom());


    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }


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

Мое приложение не входит в функцию onMessageReceived. Не знаю почему…

2 ответа

  1. Создание уведомления в классе MyFirebaseMessagingService при получении сообщения как:

    sendNotification("Hello","FCM");
    
    //method
    
            private void sendNotification(String messageTitle, String messageBody) {
                    Intent intent = new Intent(this, SplashActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    
                    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(!Utils.isStringNullOrEmpty(messageTitle) ? messageTitle : APP.NAME)
                            .setContentText(messageBody)
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
    
                            .setContentIntent(pendingIntent);
                    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.notify(1, notificationBuilder.build());
       }
    
  2. в вашем onTokenRefresh()добавить sendRegistrationToServer(refreshedToken)и реализовать это.