5

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
  • 912
  • 1
  • 12
  • 17

4 Answers4

4

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.

Ravi
  • 912
  • 1
  • 12
  • 17
  • Thanks, I followed above steps and added debug SHA1 from Android Studio and placed google-jsonconfig file to android studio > Generated Signed apk but its still not working. Working fine if I have my device wired to computer. Any idea what I might be doing wrong? – Newbie Developer Dec 18 '18 at 18:09
  • When your device is wired to a computer means the apk installed on it is a debug apk and you have provided your debug SHA1 fingerprint is right that's why its working but in order to get push notification in relase build you need to add your release fingerprint also to the firebase. I am editing my above comment and adding how to get your release fingerprint. – Ravi Dec 20 '18 at 04:12
0

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.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • I'm sry I do not have idea what they are. Could you provide any resource where I can generate/a dd them please? That'd be really great. – Newbie Developer Dec 18 '18 at 07:59
  • https://stackoverflow.com/questions/37562277/android-export-single-key-alias-from-keystore – Martin Zeitler Dec 18 '18 at 08:05
  • Thanks martin, I got debug SHA1 key from android studio and used KeyStore Explorer tool to extract SHA1 ffrom that signed to get SHA1 for release I believe. Pasted both of those to Firebase and updated android studio with new google json file > generated another signed pk but still its not working. – Newbie Developer Dec 18 '18 at 18:23
  • @NewbieDeveloper these are commonly two whole different key-stores, with two whole different keys and two whole different fingerprints. unless adding them both, this won't work. you might have to create a release key-store & key and then use that to sign the `apk` - and also add it on the Firebase console. just see the documentation: https://developer.android.com/studio/publish/app-signing – Martin Zeitler Dec 18 '18 at 20:26
0

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

Uma Achanta
  • 3,669
  • 4
  • 22
  • 49
0

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

xriminact
  • 55
  • 5