Android Push Notifications: How do I show a title? - android

We are using the Google Cloud Messaging API to push Android notifications. We are executing the https://android.googleapis.com/gcm/send endpoint with a payload similar to:
{
"registration_ids" : ["..."],
"data" : {
"message" : "You received cash back!"
}
}
The notification works and the phone shows our app logo, and the message from above. However, there is no title in the notification (above the message and to the right of the logo):
For reference, we are using Phonegap 3.3 and PhoneGap Build.

Well, a stab in the dark of adding "title" fixed it. I wasn't able to find this documented at http://developer.android.com/google/gcm/server.html#params
{
"registration_ids" : ["..."],
"data" : {
"message" : "You received cash back!",
"title" : "Title"
}
}
Now in docs:
https://developers.google.com/cloud-messaging/server#payload

Have a look at the documentation here:
{
"registration_ids" : ["..."],
"notification" : {
"body" : "You received cash back!",
"title" : "Title",
"icon": "ic_notification" // replace with your own
}
}
Edit: cloud messaging is now deprecated and this example is no longer valid. The link to the updated documentation is at firebase

its working my side
$message = array("message" => $pushMessage,"title" => "My Notification");
$fields = array(
"registration_ids" => "device id to send notification",
"data" => $message
);

Related

FirebasePushNotificationPlugin(xamarin) - events not fire when app in background(android)

I use this plugin for my Xamarin Forms app(android).
Any event not fire when app is in background. Events work when the application is in the foreground. But app is background any event not fire, the app is just opening. But I need to pass the message parameters to the application. I'm not sure if this is a bug, maybe this is my mistake. I will be grateful for any help.
I use this api for send message:
https://fcm.googleapis.com/fcm/send
This is my payload:
{
"data": {
"id_serv" : "12",
"id_group" : "1",
"click_action":"Accept"
},
"notification" : {
"body" : "test",
"title" : "test"
},
"priority": "high",
"condition": "'g1' in topics"
}
I also subscribe to all available events - OnNotificationAction, OnNotificationOpened, OnNotificationReceived.
I also try to subscribe at the android level, but this also does not work. Any help?
When the app is in the background firebase use their notification system that way your code not called.
If you want that your code will be called you need to pass the data through the data object only and not use the notification object.
so your notification object need to be like that:
"data": {
"id_serv" : "12",
"id_group" : "1",
"click_action":"Accept",
"body" : "test",
"title" : "test"
},
"priority": "high",
"condition": "'g1' in topics"
}
More info here: FB Receive massages
i don't know anything about xamarin, but in firebase version above 16, you just got push notification data when app is running,
when app is killed: after click on notification, android open your first activity with bundle (extra data), and you must get this data and use them, so debug your app and check your first activity bundle after clicking on notification

Open a web browser from push notification without launching the application?

My application is running in the background and I send a push notification through postman using the payload below.
Now when I send this, I get the notification and when I tap it, it launches my application's Main Activity. But I want to launch the web browser which opens the link that I send in the notification body. How to do this?? Can somebody please help.
{
"to" : "MY_TOKEN",
"collapse_key" : "type_a",
"notification" : {
"body" : "https://developer.android.com/training/notify-user/build-notification#click",
"title": "Title of Your Notification in Title",
"key_1" : "Value for key_1",
"key_2" : "Value for key_2",
"click_action" : "OPEN_WEB"
}
}

Retrieve notification values from a notification in system tray android firebase FCM

I am using firebase to send notifications to my app. I want to save the notifications received on each device in a local database. The problem is, when the app is in the background and a notification is received, the method onMessageReceived() won't be called, but instead the notification would be added to the system tray as per documentation. The documentation also mentions the following for receiving a notification when the app is in background:
In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
So, when I'm trying to access the extras in the intent, the keys of the notification are not found.
My code in MainActivity.java:
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Toast.makeText(this, "Key: " + key + " Value: " + value, Toast.LENGTH_LONG).show();
}
}
The notification we are sending from the server:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
Anyone has an idea of how I can retrieve the values inside the notification?
This is the intended behavior (as you've seen in the documentation). If you're using a notification-only message payload (which you are), onMessageReceived() will only be called if your app is in foreground, if not, the System Tray will be receiving it.
The data being referred to in (emphasis mine):
In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
is the data message payload (See Message Types).
You'll have to send a payload with both notification and data. Like so:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
},
"data": {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
Also see my answer here.
All you have to do is update your notification push format.
From:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
To
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
Explanation:
There is a firebase service running by your system which works for all app and handle all push notification if it contains the "notification" tag. but if contain the "data" tag only it just handover the task to the service created by the app where you can do anything.In your case show user notification and save it to the local db. For more you read here Documentation : Handle Messages.
It's not a simple or clean solution, but you could use a NotificationListenerService to receive a callback for each notification. A further limitation is that it supports only API 18 and above.
Add this to manifest file
<intent-filter>
<action android:name=".MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Android - try to get right to left notification message order in FCM

lately I've implemented Firebase cloud messaging in my project, and everything works just fine!
Except the title and body message is in left to right order, is there any hacks that I can change it to right to left?
What do you guys suggest?
EDIT:
the current notification looks like this
but I want the title and message body to be right to left,
and rtl attribute in manifest is `android:supportsRtl="false".
and this is the current Json I send to the fire-base server
{
"collapse_key" : "demo",
"to" :"xyz",
"notification" : {
"body_loc_key" : "welcome_messages",
"body_loc_args":["غلام" , "1500"],
"title" : "عنوان متن"
},
"data" : {
"key1" : "value1",
"key2" : "value2"
},
"time_to_live" : 3
}

GCM push notification message outside data dictionary

Is there a way to retrieve the key "message" on the device when sending notification to GCM.
{ "collapse_key" : "score",
"data" : { "k1" : "v1" },
"message" : "Hello World!",
"registration_ids" : [ "DEVICE_REGISTRATION_ID_GOES_HERE" ]
}
From the intent.getExtras(), I'm able to receive the following keys "collapse_key", "from", "k1" and their corresponding values as well. However I do not get key "message". If I do not add anything in the "data" dictionary, I still get push notification without "k1" (as expected).
Push to GCM does not result in errors. Does GCM drop keys not mentioned in "data" dictionary?
Yes the way you have it won't work. Anything you want to add, make it part of the data object:
{
"collapse_key": "score",
"data": {
"k1": "v1",
"message": "Hello World!"
},
"registration_ids": [
"DEVICE_REGISTRATION_ID_GOES_HERE"
]
}

Categories

Resources