Push Notifications in Android Platform

Push Notifications in Android Platform

Google's official answer is the Android Cloud to Device Messaging Framework (deprecated) Google Cloud Messaging(deprecated) Firebase Cloud Messaging

It will work on Android >= 2.2 (on phones that have the Play Store).

How to send a notification to specific platform?

Update: A recent feature was added for FCM that gives an option to provide specific params for specific platforms, called Platform Overrides:

Customizing a message across platforms

Messages sent by the FCM v1 HTTP protocol can contain two types of JSON key pairs:

  • a common set of keys to be interpreted by all app instances that receive the message.
  • platform-specific blocks of keys interpreted only by app instances running on the specified platform.

Platform-specific blocks give you flexibility to customize messages for different platforms to ensure that they are handled correctly when received. In many scenarios, it makes sense to use both common keys and platform-specific keys in a given message.

When to use common keys

  • Whenever you're targeting app instances on all platforms — iOS, Android, and web
  • When you are sending messages to topics

The common keys that are interpreted by all app instances regardless of platform are message.notification.title, message.notification.body, and message.data.

When to use platform-specific keys

  • When you want to send fields only to particular platforms
  • To send platform-specific fields in addition to the common keys

Whenever you want to send values to specific platforms only, don't use common keys; use platform-specific key blocks. For example, to send a notification to only iOS and web but not Android, you must use two separate blocks of keys, one for iOS and one for web.

When you are sending messages with specific delivery options, use platform-specific keys to set them. You can specify different values per platform if you want; but even when you want to set essentially the same value across platforms, you must use platform-specific keys. This is because each platform may interpret the value slightly differently — for example, time-to-live is set on Android as an expiration time in seconds, while on iOS it is set as an expiration date.

Example: notification message with platform-specific delivery options

The following v1 send request sends a common notification title and content to all platforms, but also sends some platform-specific overrides. Specifically, the request:

  • sets a long time-to-live for Android and Web platforms, while setting the APNs (iOS) message priority to a low setting
  • sets the appropriate keys to define the result of a user tap on the notification on Android and iOS — click_action, and category, respectively.
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Match update",
"body":"Arsenal goal in added time, score is now 3-0"
},
"android":{
"ttl":"86400s",
"notification"{
"click_action":"OPEN_ACTIVITY_1"
}
},
"apns": {
"headers": {
"apns-priority": "5",
},
"payload": {
"aps": {
"category": "NEW_MESSAGE_CATEGORY"
}
}
},
"webpush":{
"headers":{
"TTL":"86400"
}
}
}
}

See the HTTP v1 reference documentation for complete detail on the keys available in platform-specific blocks in the message body. For more information about building send requests that contain the message body, see Build Send Requests.


I remember answering a similar question before but cant seem to find it. There is currently no option to specify which platform a message would be sent. The simplest way you could do this is by using Topics Messaging.

Everytime the token is generated for the first time, you determine from your client app the Platform type and subscribe them to the corresponding topic (e.g. topics/(Android/iOS)_<Your App Name>), then sned the messages as needed.

It's also good to keep track of the registration tokens from your Server, if you're using Firebase DB, you could put them inside a node:

/pushTokens
/android
/{userId} : string
/ios
/{userid}: string

This would let you check from your backend side and adjust your payload as needed when sending single messages.

Two Push Notification services in android

When you use two Gcm clients with Gcm broadcast receiver in your own project - the last broadcast receiver registered will get the msgs coming from the Gcm servers. in other words - there will always be only one broadcast receiver that will get these msgs . In your case - the second service you added is receiving the Gcm msgs because it was registered last, probably handling it's own msgs and discarding the others.

Had a similar similar problem in my app where I used a lib that used Gcm msgs and my app was using Gcm as well. after adding the services and broadcast receivers in the app's manifest - the lib stopped receiving Gcm msgs.

The solution is not elegant - In the onReceive function of the broadcast receiver - I distinguished between Gcm msgs targeting my app and those targeting the lib in my broadcast receiver , handled the ones for my app as they should and for the ones targeting the lib - I start a new intent with the extras data , which triggered the lib's broadcast receiver. I could take this approach because I control the code for both the app and lib.

Hope this helps.

Is it possible to store the text in push notifications from an android app (not owned by me) in Google cloud?

As far as I am aware, the only way to directly integrate Android App Notifications with Google Cloud Platform is through Firebase Cloud Messaging [1]. However the flow is in the opposite direction (from GCP to Android), and some dependencies need to be present in the Android App folders (like google-services.json file of the Cloud Project needs to be present in the App folder). For that to happen, you need to have ownership of the app.

You can use Google Cloud Storage on Android [2] to transfer the notification data / payload, but still you need to have an app for that.

[1] https://firebase.google.com/docs/cloud-messaging

[2] https://firebase.google.com/docs/storage/android/start

Quickblox - How can I push notification to both platform Android and iOS?

You don't need to pass a push type param if you want to send a push to all platforms

Solution N1 (only text):

    QBEvent event = new QBEvent();
event.setUserIds(userIds);
event.setType(QBEventType.ONE_SHOT);
event.setEnvironment(QBEnvironment.DEVELOPMENT);
event.setNotificationType(QBNotificationType.PUSH);
//
event.setMessage("This is simple generic push notification!");

Solution N2 (with custom parameters):

    QBEvent event = new QBEvent();
event.setUserIds(userIds);
event.setType(QBEventType.ONE_SHOT);
event.setEnvironment(QBEnvironment.DEVELOPMENT);
event.setNotificationType(QBNotificationType.PUSH);
//
// generic push with custom parameters - http://quickblox.com/developers/Messages#Use_custom_parameters
JSONObject json = new JSONObject();
try {
json.put("message", "This is generic push notification with custom params!");
json.put("param1", "value1");
json.put("ios_badge", "4"); // iOS badge value
} catch (Exception e) {
e.printStackTrace();
}
event.setMessage(json.toString());

More examples in our push notifications snippets
https://github.com/QuickBlox/quickblox-android-sdk/blob/master/snippets/src/main/java/com/sdk/snippets/modules/SnippetsPushNotifications.java#L217

and in documentation
http://quickblox.com/developers/SimpleSample-messages_users-android#Universal_push_notifications



Related Topics



Leave a reply



Submit