Abhishek Jha
Abhishek Jha

Reputation: 167

Unable to receive push notification from Firebase console in android release build

I am able to receive FCM notifications from Firebase console in debug build but unable to receive FCM push notification in Android release build.

I have added the SHA-1 Key in firebase console and downloaded google-services.json file which i have included in the app folder of Android project. Also, i have added the below line of code in the proguard rules but not working.

-keep class com.google.firebase.** { *; }

My Android Manifest, added service as below:

<service
   android:name=".fcm.MyFirebaseMessagingService"
   android:enabled="true"
   android:exported="false">
   <intent-filter>
          <action android:name="com.google.firebase.MESSAGING_EVENT" />
   </intent-filter>
</service>

Other dependencies have been added and is working fine in debug build but is not working in android release build, could not understand where am i missing?

Any help is appreciable, Thank You.

Upvotes: 0

Views: 2038

Answers (1)

Saad Ahmed
Saad Ahmed

Reputation: 71

Make sure your are have written FirebaseMessagingService class in proper way. You can always get push notification while you are out not using the app.
If you want notification also when you are using the app then use the code given below into you FirebaseMessagingService Class:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getData().size() > 0) {
        Map<String, String> params = remoteMessage.getData();

        sendNotification(params.get("title"), params.get("body"));
    }
}

After implementing this code try to send notification from postman using the same params like "title" and "body"

private void sendNotification(String title, String message) {

    Intent intent = null;
    if (title != null && !title.isEmpty()) {
        intent = new Intent(this, DashboardActivity.class);
    }

    if(intent != null) {
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        String CHANNEL_ID = "noti01";

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setChannelId(CHANNEL_ID)
                .setVibrate(new long[]{1000, 1000})
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(mChannel);
            }
        }

        if (notificationManager != null) {
            notificationManager.notify(0, notificationBuilder.build());
        }
    }

N.B: If you are sending notification from Firebase then you can't get the notification if your app is running in foreground.

Upvotes: 0

Related Questions