1

I am trying to migrate to GCM to FCM, but previously it's worked perfectly with GCM. but after I changed it to FCM it is not working for me.

my code is

 @Override
public void onMessageReceived(RemoteMessage remoteMessage) {

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

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

        if (remoteMessage.getNotification() != null) {
            String msg = remoteMessage.getNotification().getBody();
            L.d(TAG, "Message Notification Body: " + msg);
            if (!TextUtils.isEmpty(msg)) {
                sendNotification(remoteMessage.getData(), msg);
            }
        }
    }

    // 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.
}
// [END receive_message]

private void sendNotification(Map<String, String> data, String messageBody) {

    Intent intent = null;
    if (LocalRepository.getInstance().isAuthenticated()) {
        intent = new Intent(this, DashboardActivity.class);

        String referenceKey = data.get("ReferenceKey");
        String referenceValue = data.get("ReferenceValue");
        L.d(TAG, "referenceKey:" + referenceKey + ", ReferenceValue:" + referenceValue);

        if (!TextUtils.isEmpty(referenceKey)) {
            PromotionData promotionData = new PromotionData();//item id

            boolean hasPromotionId = false;
            if (!TextUtils.isEmpty(referenceValue) && TextUtils.isDigitsOnly(referenceValue)) {
                promotionData.promotionId = Integer.parseInt(referenceValue);
                hasPromotionId = true;

            } else {
                intent.putExtra("key", referenceKey);//add this to DashboardActivity intent
            }

            switch (referenceKey) {
                case Repository.ModuleCode.BRAND:
                    intent = new Intent(this, WebViewActivity.class);
                    intent.putExtra("url", referenceValue);
                    intent.putExtra("browser", false);
                    break;


                case Repository.ModuleCode.PROMOTION:
                    if (hasPromotionId) {
                        intent = new Intent(this, PromotionDetailActivity.class);
                    }
                    break;

            }
            intent.putExtra("data", promotionData);
        }
    }

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

}

My manifest is

 <service
        android:name=".fcm.MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">

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

    <service android:name=".fcm.MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>


</application>

also I have added these in the build.gradle

apply plugin: 'com.google.gms.google-services'

also I have added this in the project gradle

 dependencies {
    classpath 'com.android.tools.build:gradle:2.2.2'
    classpath 'com.google.gms:google-services:3.0.0'
}

I have checked the already exist link and do the changes but it's not receiving the notification in my app. these the link I checked

Firebase cloud messaging notification not received by device

please suggest the solution for this. thanks in advance.

Community
  • 1
  • 1
AngelJanniee
  • 613
  • 1
  • 11
  • 30
  • What is your fcm payload? How do you send notification? – Muzaffer Nov 14 '16 at 11:03
  • String referenceKey = data.get("ReferenceKey"); String referenceValue = data.get("ReferenceValue"); These are the payload. but now I have tested through the FCM console. – AngelJanniee Nov 14 '16 at 11:11

0 Answers0