0

I implemented Firebase as indicated in Google documentation. I send message from Firebase Console. But notifications don't come on my device. Can you help me? Thank you in advance.

In my `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>

In 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);

}
}

In 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());
    }
}
}

My app does not go into the function onMessageReceived. I don't know why...

JokeRaes
  • 21
  • 6

2 Answers2

0

Create notification in MyFirebaseMessagingService class when message received as:

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());
   }
Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
Androider
  • 3,833
  • 2
  • 14
  • 24
0

in your onTokenRefresh() add sendRegistrationToServer(refreshedToken) and implement this.

Badou
  • 199
  • 2
  • 4
  • Do you know what to write in sendRegistrationToServer(refreshedToken)? – JokeRaes Nov 07 '16 at 12:51
  • [https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MyFirebaseInstanceIDService.java] – Badou Nov 07 '16 at 13:02
  • use this link : https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MyFirebaseInstanceIDService.java – Badou Nov 07 '16 at 13:06