Newbie Developer
Newbie Developer

Reputation: 65

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

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.

Upvotes: 5

Views: 5375

Answers (4)

xriminact
xriminact

Reputation: 73

I solved this by changing my Server key from Firebase console -> Project Settings -> Cloud Messaging -> Server Key

This is my piece of Code that needed the change

public interface APIService {
    @Headers(

            {
                    "Content-Type:application/json", "Authorization:key=(Find this key from Cloud messaging tab in firebase)"

            }
    )

    @POST("fcm/send")
    Call<MyResponse> sendnotification(@Body Sender body);
}

Thanks, hope this helps you

Upvotes: 0

Uma Achanta
Uma Achanta

Reputation: 3739

try to follow below steps

->generate sha1 for debug and release keystore files

keytool -list -v -keystore {keystore_name} -alias {alias_name}

eg:

keytool -list -v -keystore C:\Users\me\Desktop\release.jks -alias sample

->Add those sha1 to Firebase consolole

Upvotes: 0

Ravi
Ravi

Reputation: 1102

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.

Upvotes: 4

Martin Zeitler
Martin Zeitler

Reputation: 76799

make sure to have both fingerprints added on the console - from the debug and release key.

as you describe it, this sounds quite alike only the debug key had been added there ...

once added, the google-services.json needs to be downloaded & updated, to reflect that.

Upvotes: 0

Related Questions