chandler767 / Android-Push-Notifications

Android push notification example using Firebase Cloud Messaging and PubNub

Home Page:https://www.pubnub.com/products/mobile-push-notifications/?devrel_gh=android-push-notifications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Android Push Notification Example using Firebase Cloud Messaging and PubNub.

When building mobile applications in Android (and mobile applications in general), it is tough to think of a use case where you don’t need to communicate with mobile devices to sync data and notify users. For example, let’s consider the following scenario. You are developing a realtime sports application which feeds its users with critical news during a game. A key feature to the app would be to send reliable push notifications to the users as soon as their favorite team scores.

Push Notification Powered By PubNub

How is PubNub Used With Push Notifications?

If you are using PubNub Data Streams, you are already communicating in realtime with all of your devices – but if you want to send push notifications for a native Android experience, FCM is a great ally.

If your application is destined for multiple platforms, the PubNub SDK makes your life easier by supporting Apple Push Notification Service (APNs) and FCM out of the box. Even if you don’t support multiple platforms yet, using the PubNub support for FCM will streamline your code by using a single set of libraries for PubNub Data Streams as well as FCM notifications.

PubNub has an easy integration with FCM. A PubNub channel replaces the third party servers which send these notifications to Google’s FCM servers. You may have any registered endpoint send a push notification to your connected devices.

This allows the application to send push notifications to devices registered on specific channels in no time. It combines the native Android Notification API from Google with the realtime data stream network of PubNub. Your notifications will be natively understood by any Android device. If you already have FCM on your application and are considering using PubNub, you’ll hardly have to change anything about your app, most likely removing code!

Going back to the sports news app scenario, you can have your sports critic send the score in realtime from his phone and trigger a push notification to the rest of the audience’s devices. PubNub makes it simple – additional servers that you have to operate can be ruled out of the equation!

This project demonstrates how to setup FCM push notifications, enable PubNub push notifications, and configure the PubNub Android SDK to receive push notifications. Learn more from the tutorial Sending Android Mobile Push Notifications with FCM and PubNub.

PubNub Blog

Sending Push Notifications From PubNub

PubNub Debug Client

It’s important to note that the devices don’t need to subscribe to a channel to receive notifications for that channel. The devices only need to register to the channel for push notifications and then they will receive push notifications for any messages that have push notifications keys for that device type.

  • Associated FCM devices will receive only the data within the pn_gcm key.
  • Native PubNub subscribers will receive the entire object literal, including the notification keys.

In this app the device is registered to the "Notifications" channel. Any messages sent to that channel with pn_gcm keys will be sent as a push notification to registered devices.

The easiest way to test push notifications is to send them from the PubNub Debug Console from within the PubNub Admin Dashboard.

Go to your PubNub Admin Dashboard, select your app, and then select your keyset.

Click “Debug Console” and create a client with “Default Channel” set to Notifications.

With the client you just created you can send a notification to your device by sending a message containing the pn_gcm keys.

Send an Android push notification: {"pn_gcm":{"notification":{"body":"Hello World."}}}

Hello World Android Push Notifications Powered By PubNub

Refer to the troubleshooting guide if you have issues sending push notifications.

Have suggestions or questions about the content of this post? Reach out at devrel@pubnub.com.