Responding to a push notification using Parse - android

I'm using the Parse SDK to receive push notifications in my Android app. My backend sends a JSON formatted string with the contents of the push notification to be displayed in the notifications bar.
What I want to do is parse this JSON and depending on it's content, take the user to different parts of the app (Activities). I'm able to receive and parse the JSON fine, but how can I handle the actual action which is performed when the user taps on the notification in the notifications bar?
I know that I can configure the Parse SDK to open up different activities when I subscribe to a specific channel, like this:
PushService.subscribe(this, "testing", ActivityA.class, R.drawable.ic_push);
PushService.subscribe(this, "testing2", ActivityB.class, R.drawable.ic_push);
// etc
But this doesn't really help me, since I won't know which Activity to open until I actually receive the push notification and parse the JSON. So, what I'm looking for is something like:
public class PushReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
if(json contains id = 0) {
// open ActivityA when the user opens this notification
} else if(json contains id = 1) {
// open ActivityB when the user opens this notification
}
} catch (JSONException e) {
Log.d("", "JSONException: " + e.getMessage());
}
}
}
Any ideas how this can be accomplished using Parse? Thanks.

This is not a direct and good solution but i think it does the work (haven't tried it myself). You can use that broadcast receiver and call PushService.setDefaultPushCallback(context, ActivityAorB.class); based on your JSON data. This will set a default activity to be called when the push message is received so that this activity will open when the user taps on the notification.
Be sure to add this receiver to your manifest.
More info about broadcast receiver on parse

Related

FCM from multiple senders but within the same app

send multiple notifications using FCM, but within the same server side, saying that there is multiple notification types, probably with different data payloads and different notification titles, i want the the notification manager to be able to differentiate between each notification on the client side and send them to different Notification Channels?
EDIT
I tried setting notification_id and it does not work.
EDIT:
I can add a Data payload and restrict the notification in one of my classes and add a switch statement which will divide according to my Notification but i need to handle the data in the background and not foreground.
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
notificationSwitch(remoteMessage);
}
private void notificationSwitch(RemoteMessage remoteMessage) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
if (notification.getTitle().equals("TypeOne")){
TypeOneSendNotification(remoteMessage);
}
else{
TypeTwosendNotification(remoteMessage);
}
}
//Ofcourse we are able to switch based on the title, or different data payloads
EDIT:
I converted into a data payload and edited the notification title to be able to switch the above edit
Create the notification_key, which identifies the device group by mapping a particular group (typically a user) to all of the group's associated registration tokens. You can create notification keys on the app server or on Android client apps.
please check this link
https://firebase.google.com/docs/cloud-messaging/android/device-group
I got this working by implementing a switch statement on the title is merely a hack but it definetly works
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
notificationSwitch(remoteMessage);
}
and in my notification switch function as i already have remoteMessage as an argument i will just implement my senders.
private void notificationSwitch(RemoteMessage remoteMessage) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
if(notification.getTitle() != null){
switch (notification.getTitle()) {
case "SenderOne":
SenderOneSendNotification(remoteMessage);
break;
case "SenderTwo":
SenderTwoSendNotification(remoteMessage);
break;
case "SenderThree":
SenderThreeSendNotification(remoteMessage);
break;
case "SenderFour":
SenderFourSendNotificaation(remoteMessage);
break;
default:
DefaultSender(remoteMessage);
break;
}
}
Answering the question in the title: FCM from multiple senders but within the same app
If sender == Firebase Project then I don't think it is possible.
If sender == a client inside the same Firebase Project (like an iOS app, multiple servers, another Android app), then you need to create those apps inside your Firebase Project (for iOS and Android apps) and use the server key (for the servers)
If sender == function (like "Update Entity A", "show notification for promotion", etc), then you can use the data field inside the notification. We usually add an action field inside the data field. Both the receiver (iOS or Android app) and the sender (usually a server, but can also be a mobile app) know the list of possible actions. Receivers know what to do when they receive them (if app receives "Update Entity A", then it starts the EntityAUpdateService).

How to collapse multiple notifications into a single one using Firebase?

I have function in Firebase Cloud Functions which is used to send notifications to specific users within my app and has as the notificationContent the following code:
const notificationContent = {
notification: {
title: "My Notification Title",
body: "My Notification Body",
icon: "default",
sound : "default"
}
};
I have tried to use collapse_key: "unique_key" but it has no effect. I read the has an effect only when the device is offline. I also have used a tag: "unique" but every time a new notification arrives, it will override the oldest one.
I there any way in which I can achieve this with Firebase? If I receive more then one notification, to be grouped in a single one?
Thanks in advance!
If you want to use more customizable and advanced notification features.
You should only send FCM with data payload, and create notification at android client side.
Remember that if you send FCM with notification payload or notification + data payload, the notification will be created by android core system and BroadcastReceiver's onReceive method won't being called if your app is on background.
If you send FCM with data payload, it will call onReceive all the time, so you can produce custom notification manually at android client side. (most app uses latter method.)
I hope this link would be helpful.
I had this same confusion and realized I misunderstood what collapseKey and tag are for.
collapseKey will limit the number of notifications a client receives while they're offline, tag is what will stack notifications together in the drawer.
So for a typical cloud function, it should look like this:
const notification = {
notification: {
'title': 'Interesting title',
'body': 'Hello, world'
},
'data': {
'whatever': whatever,
},
'android':{
'collapseKey': collapseKey,
'priority': 'high',
'notification': {
'tag': tag,
}
},
'token': fcmToken
};
admin.messaging().send(notification)
Note that the "tag" parameter sits inside of the android notification, not the top-level notification.
The easiest and most flexible solution is to extend the FirebaseMessagingService and handle the notification yourself. But first instead of using notification on your notificationContent in your cloud function, you have to change that to data so that you send a data message instead of a notification message. The difference is that the notification message will have an implicit collapse key (the package name of the app), while the data message won't have one. But the data message needs to be handled on the client or else it won't be displayed.
Here's a sample of what you'll need for your FirebaseMessagingService:
public class MyFCMService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getNotification() != null) {
//this notification was sent from the Firebase console, because in our cloud function, we are using the DATA tag, not the notification tag
//so here we have to handle the notification that was sent from the console
...
} else if (remoteMessage.getData().get(KEY) != null) {
//this data message was sent from our cloud function
//KEY is one of the keys that you are using on the cloud function
//in your example, you are using the keys: title, body, icon, sound
//display the notification to the user
...
notificationManager.notify(TAG, ID, notificationBuilder.build());
//you have to use the same TAG and the same ID on each notification if you want your 2nd notification to simply update the text of the first one, instead of showing as a new notification
}
}
}
PS. When you send a notification from your cloud function (well if you use the data tag, it's actually a data message, not a notification message), then this method will be called, regardless if the app is in the background or in the foreground. HOWEVER, when you send a notification from the firebase console, this method will be called ONLY if the app is in the foreground. If the app is in the background, the Firebase SDK will handle the notification and show it to the user. In some cases, it makes sense to show a notification only when the user is not running the app, for example if you want to advertise some new features of the app. In that case, what you can do is use a unique tag on the notification console (e.g. "display_in_foreground") and check that tag on the client. If you have set that to true, you can show the notification even to users that are currently running the app, or if it's false you can choose not to show the notification. This check will happen only if the app is in the foreground. If it's in the background, this won't be called at all and the SDK will handle to show the notification.

Unable to handle event in background push notification

I am using cordova plugin add phonegap-plugin-push plugin for push notification
In forground notification works fine.and i can handle event also.
When my app is in background then i got notification as well but on click of push notification my event is not fire.
I am using below code
$cordovaPushV5.initialize(options).then(function() {
// start listening for new notifications
$cordovaPushV5.onNotification();
// start listening for errors
$cordovaPushV5.onError();
// register to get registrationId
if (PNdeviceToken == null) //becuase registration will be done only the very first
{
$cordovaPushV5.register().then(function(registrationId) {
// save `registrationId` somewhere;
window.localStorage.setItem('PNdeviceToken', registrationId);
$rootScope.fcmToken = registrationId;
console.log(registrationId)
alert("first time registered id -- " + registrationId)
})
} else {
$rootScope.fcmToken = PNdeviceToken;
alert("already saved registered id -- " + $rootScope.fcmToken)
}
});
$rootScope.$on('$cordovaPushV5:notificationReceived', function(event, data) {
console.log(event)
console.log(data)
})
When i tap on background push notiction then $cordovaPushV5:notificationReceived event not fire, How can I solve this problem?
How can i handle background push notification event?
I had the Same issue and got it resolved it after 2 days of research.
Handling the notification events is same whether the app is in foreground or background.
We have to set "content-available" : "1" in the data field while pushing notifications. Else it wont call notificationReceived event if app is in background.
Also note this is not possible as of now through Google Firebase Console.
We have to send our custom payload messages (data or notification or both) seperately using any one of the firebase servers.
Detailed info can be found on the plugin's GitHub Docs Page on background notifications.
Quoting from there -
On Android if you want your on('notification') event handler* to be called when your app is in the background it is relatively simple.
First the JSON you send from GCM will need to include "content-available": "1". This will tell the push plugin to call your on('notification') event handler* no matter what other data is in the push notification.
*on('notification') event handler = $cordovaPushV5:notificationReceived event in your case.
See this answer for sending custom payload messages using PHP and NodeJS

Handle Firebase topics notification on Android

I'm trying to send push notifications throught FCM services.
In my MainActivity I write this code:
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// checking for type intent filter
if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
// gcm successfully registered
// now subscribe to `global` topic to receive app wide notifications
FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
displayFirebaseRegId();
} else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
// new push notification is received
String message = intent.getStringExtra("message");
Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
}
}
};
In order to subscribe user to a topic.
From backend I make a call to FCM service at this link : https://fcm.googleapis.com/fcm/send
in which I pass this JSON:
{
"to":"/topics/global",
"data":{
"title":"test",
"is_background":false,
"message":"testmessage",
"image":"",
"payload":{
"team":"Test",
"score":"5.6"
},
"timestamp":"2017-05-23 11:55:35"
}
}
and I get this response:
{\"message_id\":8863205901902209389}
But my device doesn't show any notifaction, exept if I use Firebase console with "user segment" or "single device" . Also in Firebase console doesn't works "topic" way.
Thank you in advance for any response.
There are two types of FCM messages.
Notification Messages
Data Messages
On the client side, Notification messages are handled by FCM and automatically displayed in the notification window. If you are using Data Messages, your app needs to handle the received message and create a notification.
The sample payload in your question is a data message and hence it is not displayed in notification (I assume you didn't do any handling). The notifications sent via FCM console are always notification messages and hence those are automatically displayed.
Refer to this FCM page for more details on this.

What notification system to use in a chat application?

I'm developing a chat application using Firebase Database in Android.
I've already done the core (chat and user's list activities), but I have yet to do the notification system.
I wanted to implement Google FCM as I am using firebase, but I find an hole in it, as it seems that I can not handle at all notifications delivered to iOs apps that are not in foreground.
As found in the Data notification documentation
On iOS, FCM stores the message and delivers it only when the app is in the foreground and has established a FCM connection. On Android, a client app receives a data message in onMessageReceived() and can handle the key-value pairs accordingly.
And I need to catch the data notification even when the app is in background, I need that specifically because I want to update my badge counter on the app icon to let the user know how many unread messages he has.
I'm now tryng the OneSignal solution can receive notification even when in background, it's free and interfaces with GCM. I'm sad to not stay with Google but if I can't update the badge count using FCM I have to look to an other side.
Any consideration will be appreciated.
Use below code for get badge from server when app is in background or not running because your FirebaseMessagingService is running.
If you have any doubts regarding show badge on app icon than comment it because just yesterday i did show badge on app icon.
public class Custom_FirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FirebaseMsgService";
//private Activity strPushClickDesti;
String activityName;
#Override
public void zzm(Intent intent) {
Log.i("uniqbadge", "zzm");
Set<String> keys = intent.getExtras().keySet();
for (String key : keys) {
try {
Log.i("uniq", " " + key + " " + intent.getExtras().get(key));
if (key.equals("badge")) {
String cnt = intent.getExtras().get(key).toString();
int badgeCount = Integer.valueOf(cnt);
Log.i("uniq", " badge count " + badgeCount);
ShortcutBadger.applyCount(this, badgeCount);
Log.i("uniq", " " + "end");
}
} catch (Exception e) {
Log.i("uniqbadge", "zzm Custom_FirebaseMessagingService" + e.getMessage());
}
}
super.zzm(intent);
}
There is in fact a feature explored by OneSignal to achieve this behavior. It is the content-available value in the notification payload. The good news is that is native to iOS 7 and above. So if you can customize the payload in FCM, it is possible. Just activate remote-notification in background modes and handle the badge update in application(didReceiveRemoteNotification:fetchCompletionHandler:) and you are good to go.

Categories

Resources