Android: Firebase Cloud Messaging not sending notification to build .apk

Newbie Developer :

Push notification is working when I connect phone to Android Studio via USB cable, but when I generate build .apk and install it on phone, app is not receiving any push notifications.

I'm new to android and trying push notifications for first time.

Code in Manifest.xml

<service
            android:name=".MyFirebaseInstanceIDService">
            android:stopWithTask="false">
            <intent-filter>

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

Code for class extending FirebaseMessagingService :

public class MyFirebaseInstanceIDService extends FirebaseMessagingService {
    @Override
    public void onNewToken(String s) {
        Log.e("NEW_TOKEN", s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Map<String, String> params = remoteMessage.getData();
        JSONObject object = new JSONObject(params);
        Log.e("JSON_OBJECT", object.toString());

        String NOTIFICATION_CHANNEL_ID = "Nilesh_channel";

        long pattern[] = {0, 1000, 500, 1000};

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Dream",
                    NotificationManager.IMPORTANCE_HIGH);

            notificationChannel.setDescription("");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(pattern);
            notificationChannel.enableVibration(true);

            mNotificationManager.createNotificationChannel(notificationChannel);
        }

        // to diaplay notification in DND Mode
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
            channel.canBypassDnd();
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

        notificationBuilder.setAutoCancel(true)
                .setColor(ContextCompat.getColor(this, R.color.colorAccent))
                .setContentTitle(getString(R.string.app_name))
                .setContentText(remoteMessage.getNotification().getBody())
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_action_name)
                .setAutoCancel(true);


        mNotificationManager.notify(1000, notificationBuilder.build());
    }
}

MainActivtiy.java class has nothing linked to it related to Firebase.

Any suggestion will be appreciated. Thanks.

Ravi :

Please login to your firebase console and open your project and then find the project settings like this : enter image description here

and then click on project setting and go down on that page you will find your app and a option to add fingerprint

Add Fingerprint

enter image description here

Just add your fingerprint there and click on save.

When you have added the fingerprint just download the google-jsonconfig file from there

Download the latest config file

And put that config file inside your project and build your app again.

Hope this time everything works fine.

And, if you need further help getting your SHA1 key(Fingerprint) please comment again I will try to help you.

Now, to get your release and debug fingerprint from android studio follow this:

1.Go to gradel section located on right hand side of your android studio.

enter image description here

2. You will find there : app click on that and go to Task->android->signingReport

enter image description here

3.Double click on signingReport and wait until your signingRreport are generated inside Run tab located at bottom of the android studio.

4.Now in the you will have a format like this:

Variant: release
Config: release
Store: /home/hp/.android/release.keystore
Alias: AndroidReleaseKey
MD5: Your MD5 key here(xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx)
SHA1: Your fingerprint key here(xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx)
Valid until: Thursday, 20 August, 2048

Variant: debug
Config: debug
Store: /home/hp/.android/debug.keystore
Alias: AndroidDebugKey
MD5: Your MD5 key here(xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx)
SHA1: Your fingerprint key here(xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx)
Valid until: Thursday, 20 August, 2048

From the release variant, copy your SHA1 key and add it to firebase. I hope this will solve your problem now.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=102083&siteId=1