I check the JSON that is comming from parse push that I'm sending from parse push and if it match with my filter, I save it in database otherwise if it doesn't match, I want that user don't see it's notification at all.
checking and saving are working good, but I can't dismiss (abort) it when it's not matched.
I tried this.abortBroadcast(); at onReceived and also onPushReceive but it doesn't work.
You should not abort it, just you have to use a correct JSON format, like this:
{
"data": {
"message": "Hello! World.",
"title": "my title"
},
"is_background": false
}
is_background determine whether to show notification or not.
Related
I'm using com.google.firebase:firebase-messaging:20.0.0 on Android. When I try sending a notification like the one below it always shows the normal title and body instead of title_loc_key and body_loc_key when they are presented in the app! The fun thing is that it's not the case in iOS. In iOS it will always try to first show the localized resources and if it couldn't find them, it will default to normal ones.
{
"to" : "f6_numko7IQ:APA91bFrTN0fmThFDeAFy2...",
"collapse_key" : "type_a",
"notification" : {
"title_loc_key": "resource_name_1",
"body_loc_key": "resource_name_2",
"title": "Default Title",
"body": "Default Body!"
}
}
Is this a known issue? Is there a get around for it?
The solution is to remove the "title" and "body" entries. I know it should work like on iOS but currently it's not.
{
"to" : "f6_numko7IQ:APA91bFrTN0fmThFDeAFy2...",
"collapse_key" : "type_a",
"notification" : {
"title_loc_key": "resource_name_1",
"body_loc_key": "resource_name_2",
}
}
For Android: Using the "v1" api from firebase remove "title" and "body" from root "notification: {....}" and add into the "android": {
"notification": {
"title_loc_key": "push_notification_title",
"body_loc_key": "push_notification_body"
}
},", should work.
But you need to keep in mind the following:
The push notification will be displayed by default by firebase SDK based on the com.google.firebase.MESSAGING_EVENT, when the app is in the background --> opened (the process is in the background) and Notification permission is allowed. Also, you need to be sure that your app has already a channel created with the default_channel_id you defined in the manifest file. For this case, you will not be notified of the onMessageReceived callback provided by "FirebaseMessagingService".
When the app is in the foreground you will not receive any firebase notification, visible on the screen. Based on the Channel you have created at point 1) you need to trigger the notification display if required, by creating a NotificationBuilder .... and displaying it. This catch must happen inside the: onMessageReceived callback provided by "FirebaseMessagingService"
When the app is closed and notification is allowed the push notification will be displayed by default by firebase SDK based on the com.google.firebase.MESSAGING_EVENT. However, if the notification received by the device contains some data: {} payload that can be handled into the onCreate lifecycle method from activity by accessing the this.intent.extras. You will not be notified of the onMessageReceived callback provided by "FirebaseMessagingService".
If notification is disabled you will not get any push notification displayed visually but when the app is in the foreground you will be notified into the onMessageReceived callback provided by "FirebaseMessagingService". When the app is closed or in the background, you will get nothing.
We are using firebase push notification in Android and in iOS. The push is sent using FCM REST API call. Push type is notification with extra data node.
Here is a sample payload:
{
"notification" : {
"title": "title text",
"body": "message body text",
"sound": "default"
},
"data": {
"messageType": "xxx"
},
"to": "yyy",
"priority": "high",
"time_to_live": 0
}
This type of push notification does not show a heads up display when the app is in background and phone is on. -- Notification just are added to notifications bar but are not sneak peaked to user at the top of the screen. -- no matter if the current app is full screen app or not.
One solution that I have tried and is working is to shift to pure data message where we will not send any notification node, but just the data node and write the code to show notification ourselves and set notification priority to Max (ie .setPriority(Notification.PRIORITY_MAX)) on notification builder object.
But this seems to have issues on iOS where data only pushes are not received/shown to user if the app killed by user.
So is there any workaround to this? any solution that works on Android, but also does not break iOS.
You should set the NotificationPriority to PRIORITY_HIGH or PRIORITY_MAX for Android to show the notification. See Android Notification and Notification Priority for more details.
You should set these values using the constants, not using the strings. E.g.
{
token,
android: {
notification:
{
title,
body,
// See https://developer.android.com/reference/android/app/Notification#PRIORITY_MAX.
// This is deprecated in Android API level 26, but works for older versions.
notification_priority: 2,
// Always allow Android users to see the message in its entirety.
// See https://developer.android.com/reference/android/app/Notification#VISIBILITY_PUBLIC
visibility: 1
}
}
}
We are using Firebase in a SIP app to send us missed call notifications and chat notifications whenever the app was offline.
While sending and receiving works fine, we have the effect on the Android client, that 5 missed calls obv produce 5 missed-call-notifications, filling up the notification bar on the client device.
How can we merge those notifications together, to just show a single "5 missed calls" notification?
Is there any additional flag (like grouping) we can put in the data or notification part of the message?
Here is an example of our current missed call notification:
{
"to":"<<FCMToken>>",
"priority":"high",
"notification":{
"title":"<<Displayname-of-Caller>>",
"text":"<<Date-and-time-of-call>>",
"icon":"icon_notification_missed",
"click_action":"MISSED_CALL"
},
"data":{
"type":"sip-call-missed"
}
}
So what's the trick in combining them together as one?
We found the correct solution.
There are more existing keywords for the notification content.
The one we needed was "tag".
We can even localize the client-side text of the notification by supplying a resource name in loc keys.
Here is a correct message that can be bundled together:
{
"to":"<<FCMToken>>",
"priority":"high",
"notification":{
"title_loc_key":"notification_missed_call",
"tag":"MISSED_CALL",
"body_loc_key":"notification_missed_call_multiple",
"body_loc_args":["<<missed_call_count>>"],
"icon":"icon_nav_main_chat",
"click_action":"MISSED_CALL"
},
"data":{
"type":"sip-call-missed"
}
}
The tag will be merged by the client ... say: they will replace each other. Whenever a notification with a tag arrives, it replaces all other existing notifications with the same tag.
So the trick here is, to supply a running count <<missed_call_count>> (which the server has to count), so the client can show an increasing number, like "5 missed calls".
The string "%d missed calls" is stored in the client side string resource named "notification_missed_call_multiple".
With the Urban airship's library, I was able to implement the push notification ok. Which will trigger the onReceive() method every time I send push a message from a server.
But when I switch to use PushRich notification, whenever I send out a rich message, it won't triiger the onReceive() method.
I want to be able to achieve the same way it dose with the push notification in here.
I tried the richpush simple code from the website, but it seems to be having the same problem.
The fact that I think its possible its because from the sample code onReceive() method has the following code in it.
// Ignore any non rich push notifications
if (!RichPushManager.isRichPushMessage(intent.getExtras())) {
return;
}
Would it mean, it should send intent when we send rich push from server ?
Problem has been solved. After more digging into the Urban airship library. The rich message that contains HTML is actually be put into part of the push message JSON object.
Looks like such in a simple push message
{
"audience" : { "tag" : [ "tag1", "tag2" ] },
"device_types" : [ "ios" ],
"notification" : { "alert" : "New message!" },
"message" : {
"title" : "Message title",
"body" : "<Your message here>",
"content_type" : "text/html"
}
}
The "message" object is actually the Rich message itself. So therefore, catching the intent for a simple push message can from then determine if a rich message exist in the intent.
Is there a way to somehow add some data to the Android push notification by the server side?
So I can parse the added data in my app and do some actions related to it, like an Id or something like that.
Of course it must be "hidden" so the simple message is not the way I want it.
Of course there is. Every piece of data you put in the message payload can be "hidden". As the developer of an Android app, it's your decision which parts of the payload are displayed to the user and which are not.
In the server side you decide which data you wish to put in the payload of the GCM message (inside the data dictionary), and you decide what to do with it in the application.
{ "time_to_live": 108,
"data": {
"message": "you can display this message",
"id": "123456",
"some-hidden-field": "some-hidden-value"
},
"registration_ids":["4", "8", "15", "16", "23", "42"]
}