Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Azure Push Notifications Google FCM

I've followed Sending push notifications to Android with Azure Notification Hubs tutorial to implement Push notifications into my Android app using the Azure Notification Hub.

Previously, I used another tutorial to do this using the Google Cloud Messaging service with Azure Notification Hub, however I was only able to get Push notifications when I sent my app to my device from Android Studio. When I built a signed APK and installed that on my device, no push notifications came through.

After trying all day yesterday to get it to work (disabling ProGuard, trying different API keys etc) I decided to start fresh this morning. That is when I realized that Google now directs users to the Firebase Cloud Messaging when they click on GCM from the Cloud Console. So...I've implemented the Push Notifications into my app using the above tutorial and Google FCM.

It works great....but again, when I create a signed APK and install that on my device rather than sending the app to my device from Android Studio, I get no push notifications. Azure shows that the push was sent successfully, but nothing comes through.

In my push handler, I have a log to the console as seen below in the onReceive method. This gets called fine when I run the app from Android Studio, and the push comes through as it should. But when I create signed apk and run from that, the onReceive method does not get called and no push comes through.

@Override
    public void onReceive(Context context, Bundle bundle) {

        Log.d("TAG","TRIGGERED");

        ctx = context;
        String nhMessage = bundle.getString("message");
        String nhTitle = bundle.containsKey("title") ? bundle.getString("title") : "Title";
        String nhBadge = bundle.containsKey("badge") ? bundle.getString("badge") : null ;
        sendNotification(nhMessage,nhTitle,nhBadge);
        if (Main.isVisible) { Main.mainActivity.ToastNotify(nhMessage); }
    }

Someone please help me. What am I missing????? Is it because I'm installing the APK directly? Does it HAVE to be downloaded from Google Play? Other than that, I don't see what it could be.

UPDATE

As per Nikita G's suggestion, I've followed the instructions to send a test push from the command line using cURL.
The response I get back is the following (which looks like a success message) however I get no Push on my device.

{"multicast_id":6722521883082447284,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1473693614087201%06fb35f0f9fd7ecd"}]}

My cURL request looks like this, just like the tutorial shows.

curl --header "Authorization: key=XXXXXXXXXXXX" --header "Content-Type: application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"fs...Tw:APA...SzXha\"]}"

UPDATE

I just noticed that it is not just when I use a SIGNED APK. In Android Studio, if I generate a APK using the Build APK option

enter image description here

and then install using the app-debug.apk file located at myappfolder/app/build/output/apk/, the Push Notifications still do not work. they ONLY work if I send to my device from Android Studio using the Run option.

like image 807
Phil Avatar asked Sep 09 '16 14:09

Phil


People also ask

How do I send Azure push notifications?

To set up Windows Push Notification Service (WNS): In the Azure portal, on the Notification Hub page, select Windows (WNS) from the left menu. Enter values for Package SID and Security Key. Select Save.

What is difference between GCM and FCM?

FCM is a cloud platform that provides messages and push notifications for operating systems- ios and Android, and websites as well. Google Cloud Messaging is a messaging service that enables the message transfer from server to clients apps.

How do I test FCM push notifications Android?

Send a test notification messageOpen the Notifications composer and select New notification. Enter the message text. Select Send test message. In the field labeled Add an FCM registration token, enter the registration token you obtained in a previous section of this guide.

Why you use FCM notification instead of APNs?

APNs and WNS do not support multiple platforms. They are designed to work with their native platforms. But, FCM supports multiple platforms such as Android and iOS and even supports Chrome web apps.


2 Answers

After days of trying to figure this out, turns out my MyHandler class, which is the class responsible for handling pushes that come through, was not declared as public.

After changing the class to public, push now works on build and signed apks.

Would love someone to explain why this would only cause a problem when the app was installed from apk file though because that part has me stumped.

like image 167
Phil Avatar answered Oct 09 '22 14:10

Phil


One important method to follow in these kind of issues if first to try to understand whether the problem is on the Notification Hubs side or on the PNS (FCM in this case) side.

One way to do it is to first go through the ANH diagnosis guidelines. If the issue is still not resolved, it may be helpful to try to push a message directly through the PNS to see whether that works or not. (In case of FCM, you can send a test push from the command line.)

like image 34
Nikita R. Avatar answered Oct 09 '22 13:10

Nikita R.