GCM downstream messaging JSON format for iOS - ios

I have an swift/iOS9 application using GCM for it's notifications (WIP).
Application authorisations OK.
Certificates are OK.
Configuration file OK.
Everything is configured on the Apple's developpers portal for development.
This function is called when a notification is received.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
GCMService.sharedInstance().appDidReceiveMessage(userInfo);
print(userInfo.debugDescription)
}
Problem
I can only detect a notification in the previously described function when the server send the following format, and it 'works' because i have a breakpoint to detect it, else nothing happen.
{
"registration_ids" : ["regId"],
"data" :
{
"to" : "regId",
"notification" :
{
"sound" : "default",
"badge" : "2",
"title" : "anyTitle",
"body" : "anyMessage"
}
}
}
The person in charge of the webservices made this for me, and it duplicated the existing Android one (Is "registration_ids" any usefull?).
Since the behaviour wasn't the expected one, we tried this from GCM website :
{
"to" : "regId",
"content_available" : true,
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark"
}
}
The message create an error server side (invalid format for GCM?)
Do we miss something obvious?
Requests are post and headers/url:
https://gcm-http.googleapis.com/gcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
EDIT
Adding the log of the working and non working one.
{
"registration_ids":[
"regId"
],
"data":{
"to":"regId",
"notification":{
"sound":"default",
"badge":"2",
"title":"testNotif",
"body":"welcome in the Showcase Apple owner"
}
}
}
And
Exchange[
Id ID-FR-LIL-D00184-54996-1457452459441-0-5
ExchangePattern InOnly
Headers {Accept=text/html,application/xhtml+xml,application/xml;q=0.9,​*/*​;q=0.8, Accept-Encoding=gzip, deflate, Accept-Language=fr-fr, Authorization=key=AIzaSyAB_E2Op0GqShCmCmh_6ZxnwrFKoXOaIHU, beaconId=46589-47438, breadcrumbId=ID-talend2-48271-1456928459992-11-173, Cache-Control=max-age=0, CamelHttpMethod=POST, CamelHttpPath=, CamelHttpQuery=custLogin=alex#cgi.com&beaconId=46589-47438&regId=khjGINhshr4:APA91bGXuzrC3tU_jkBMZGCziqIwA9wKv1B-U4acxy68sQxvChJQvKb187o863CzKJyop1AwhP0BNo7I2SJJiWdrtnHFC42LxcBQzOo2Ah868xPde9TBFmj_FLVG8rhyH4Yl48zuQMCJ, CamelJmsDeliveryMode=2, CamelRedelivered=false, CamelRedeliveryCounter=0, CamelServletContextPath=/setBeaconEvent, Connection=keep-alive, Content-Type=application/json, custLogin=, dateEvent=20160308172331, deviceType=ios, DNT=1, Host=192.168.1.239, JMSCorrelationID=null, JMSDeliveryMode=2, JMSDestination=queue://Q.NOTIFIER, JMSExpiration=0, JMSMessageID=ID:FR-LIL-D00184-64570-1457442695117-1:52:1:1:1, JMSPriority=4, JMSRedelivered=false, JMSReplyTo=null, JMSTimestamp=1457454212345, JMSType=null, JMSXGroupID=null, JMSXUserID=null, regId=khjGINhshr4:APA91bGXuzrC3tU_jkBMZGCziqIwA9wKv1B-U4acxy68sQxvChJQvKb187o863CzKJyop1AwhP0BNo7I2SJJiWdrtnHFC42LxcBQzOo2Ah868xPde9TBFmj_FLVG8rhyH4Yl48zuQMCJ, User-Agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4}
BodyType String
Body {"registration_ids":["khjGINhshr4:APA91bGXuzrC3tU_jkBMZGCziqIwA9wKv1B-U4acxy68sQxvChJQvKb187o863CzKJyop1AwhP0BNo7I2SJJiWdrtnHFC42LxcBQzOo2Ah868xPde9TBFmj_FLVG8rhyH4Yl48zuQMCJ"],"to":"khjGINhshr4:APA91bGXuzrC3tU_jkBMZGCziqIwA9wKv1B-U4acxy68sQxvChJQvKb187o863CzKJyop1AwhP0BNo7I2SJJiWdrtnHFC42LxcBQzOo2Ah868xPde9TBFmj_FLVG8rhyH4Yl48zuQMCJ", "content_available": true, "notification":{"title": "testNotif","body": "welcome in the Showcase Apple owner" }}
]
Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
org.apache.camel.component.http.HttpOperationFailedException: HTTP operation failed invoking https://android.googleapis.com/gcm/send?custLogin=alex#cgi.com&beaconId=46589-47438&regId=khjGINhshr4:APA91bGXuzrC3tU_jkBMZGCziqIwA9wKv1B-U4acxy68sQxvChJQvKb187o863CzKJyop1AwhP0BNo7I2SJJiWdrtnHFC42LxcBQzOo2Ah868xPde9TBFmj_FLVG8rhyH4Yl48zuQMCJ with statusCode: 400
EDIT2
Thanks to Arthur advices, the notification leaves the server without error now, and I receive it.
Current format is :
{
"to":"regId",
"data":{
"notification":{
"sound":"default",
"badge":"2",
"title":"testNotif",
"body":"welcome in the Showcase Apple owner"
}
}
}
The remaining problem is that didReceiveRemoteNotification fetchCompletionHandler don't receives it if app is in background.
Also didReceiveRemoteNotification receives it when app is foreground, but if I don't display it nothing happen.
I am supposed to call something to have the 'system style' notification? Is it ready out of the box if the format is correct?
I can't find informations about this over the web.

So when you use the first format you mentioned, you receive the notification (the app stops at the breakpoint)? My only idea is that maybe your app is in foreground when you receive the notification? It should be in the background or closed otherwise you won't notice anything.

Ok so, after all the only problem was the JSON format :
{
"to":"regId",
"notification":{
"sound":"default",
"badge":"2",
"title":"testNotif",
"body":"welcome in the shop Apple owner"
}
}
This way the notification is processed by APNS and arrive refined in the correct format.

Related

How to send a big picture in push notification using Apple APN like you do with FCM?

I'm new to using Apple APN to send push notifications. With FCM (Firebase), there is an 'image' property you can use to send a big picture with your push notification. It doesn't look like there is an option to send a big picture with your push notification using Apple APN based on this documentation:
https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification
This is an example of a post request using the Apple APN:
"aps" : {
"alert" : {
"title" : "Game Request",
"subtitle" : "Five Card Draw",
"body" : "Bob wants to play poker"
},
"category" : "GAME_INVITATION"
},
Is there an property you can use in the aps object or the alert object that allows you to send a big picture with your push notification?
How do you send a big picture with your push notification using Apple APN?
Are you sending your notifications via firebase?
Then actually there should be an option, but you need then to specify the picture for the different platforms like:
const message = {
notification: {
title: 'Sparky says hello!'
},
android: {
notification: {
imageUrl: 'https://foo.bar.pizza-monster.png'
}
},
apns: {
payload: {
aps: {
'mutable-content': 1
}
},
fcm_options: {
image: 'https://foo.bar.pizza-monster.png'
}
},
webpush: {
headers: {
image: 'https://foo.bar.pizza-monster.png'
}
},
topic: topicName,
};
As I understand the documentation and the example here you can only send a link that will be loaded on receiving the picture.
Another idea could be to put the same in a custom payload in a data object within the message. But keep in mind there are some limitations to notifications:
limits for push payload size determined by Google and Apple — 4096 bytes (in iOS 8 and lower, the maximum size allowed for a notification payload is 2048 bytes [1]).
Links to further explore:
[enter link description here][1]
[enter link description here][2]
[1]: https://help.pushwoosh.com/hc/en-us/articles/360000440366-Limit-of-characters-that-can-be-sent-through-a-push-notification#:~:text=Answer%3A,is%202048%20bytes%20%5B1%5D).
[2]: https://firebase.google.com/docs/cloud-messaging/ios/send-image

How to debug background notifications in iOS?

I am trying to debug background push notifications on iOS, and more specifically to set breakpoints into code that should handle a notification to configure internationalized content using custom logic.
I set a breakpoints in my AppDelegate's userNotificationCenter(_:willPresent:withCompletionHandler:) and userNotificationCenter(_:didReceive:withCompletionHandler:).
And I also created a notification service extension and added a breakpoint to its didReceive(_:withContentHandler:) because I want to mutate the content of my notification.
When I send the following payload and my app is in the foreground, the breakpoint in userNotificationCenter(_:willPresent:withCompletionHandler:) gets a hit and everything is fine:
{
"aps" : {
"badge" : 1012,
"category" : "GENERIC_MESSAGE",
"mutable-content" : 1
},
"translations" : [
{
"LanguageCode" : "fr",
"Title" : "Aimant détaché",
"Body" : "L'aimant du capteur 002F51AB, associé à l'outil HT-1225 Hitachi Tas, a été détaché."
},
{
"LanguageCode" : "en",
"Body" : "The magnet of the tag 002F51AB, link to tool HT-1225 Hitachi Tas, was detached.",
"Title" : "Magnet detached"
},
{
"LanguageCode" : "nl",
"Body" : "De magneet van de tag 002F51AB, gelinkt aan het gereedschap HT-1225 Hitachi Tas, is losgekoppeld.",
"Title" : "Magneet losgekoppeld"
}
],
"messageId" : "90073ebb-ce51-ea11-a94c-000d3a213771"
}
But if the application is in the background and I send the exact same notification, I don't get a hit on userNotificationCenter(_:didReceive:withCompletionHandler:). Same thing when I run the notification service extension, I don't get a hit in didReceive(_:withContentHandler:)
Am I forgetting something? Is it because my notification payload doesn't have an aps alert field?
I figured it out: when I add an alert field to my push notification, even with a dummy string, then it get forwarded to my Notification Service. Otherwise it doesn't.

Push notifications on Swift

I have a really big problem with Firebase Notifications;
my problem is that i can receive notification in background mode and foreground mode, but if I terminate it(kill it from the ram) I can't receive anything.
I found that if I comment this function
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
I can receive a notification when i reopen the application after it's terminated, but i can't get a notification in background like before.
There's any way to solve it, because my application should receive notification everytime: background, foreground and when application is reopened from a terminated state
The following is an example of my notification's JSON:
{
"content_available": true,
"priority": "high",
"data": {
"priority": "SILENT",
"target": "contact",
"msgBody": "",
"msgTitle": ""
},
"to": "firebase_TOKEN"
}
I also want to say that this notification should be silent
When app is killed notification will be handled from operating system so it needs some specific key to display notification.
Like consider the below example:
{
"content_available": true,
"notification": {
"title": "has sent you a message",
"sound": "default",
"body": "Hi",
"badge": 6
},
"to": "firebase_TOKEN",
"priority": "high"
}
Here you need to replace "data" with "notification" and you also need a "title" instead of "msgTitle" and "msgBody" will be replaced with "body".
Looks like your's is a android payload which won't work with iOS.
You must required to add "Content-available" : "1", in your notification to get push notification when application killed.

FCM Push Notification "content" entry

I'm using FCM to publish push notifications to my users, which is working great so far.
Currently I'm trying to implement a Notification Content Extension to deliver customized push notification and previews, which is working great with local notifications, following this post.
As far I know, I have to set the category entry and my notification category identifier in the push notification, in order to tell iOS, which notification UI it is supposed to use.
The problem is, when I send the following message to FCM, with the category entry set, FCM erases the entry or changes it to gcm.notification.category, depending where I place the category entry (aps / data, etc.)
This way iOS never shows my custom UI / extension. Unfortunately I was not able to find any help in the FCM documentation.
Send (POST: https://fcm.googleapis.com/fcm/send):
{
"notification": {
"title": "Good Morning",
"body": "Wake up Jack!",
"badge" : 1,
"sound" : "horn.aiff",
"category" : "Cheers" <-- Is going to be deleted / changed
},
"data" : {
"time" : "2018-01-19 23:00:00",
...
},
"mutable_content" : true,
"priority" : "high",
"registration_ids" : [
"abcdefg123456"
]
}
Received:
{
aps = {
alert = {
body = "Wake up Jack!";
title = "Good Morning";
};
badge = 1;
"mutable-content" = 1;
};
"gcm.message_id" = "0:1516392279506894%dc84760ddc84760d";
"gcm.notification.category" = "Cheers"; <-- not working
}
The category APNS parameter FCM counterpart is click_action.
When adding in a custom parameter (using the data message payload), it is handled differently for iOS and is often included outside of the aps payload (like in the sample you provided).

FCM silent push notification structure for iOS?

I need example of notification payload for silent notification, I tried this below structure, but it's not working
{
"notification" : {
"body" : " Survey list updated",
"content-available:" : true,
"data" : {
"isNewUpdateAvailable" : "easysurvey.survey_list_updated"
}
},
"to" : "f6PwToRUxk0:APA91bG7bSWoKsjHXVmXaiDEnFXA2x2jEOMSO6eGCqPv1fRd-dctNLDEabRq-0So_obuPGFqOFTSLJl5FFyuOuXKBXh-n89BmzzXenRTxoirY9Y1c6-J9MxpDp0ojHL2xm1law0V6gg3"
}
Using this structure, I am able to receive notifications, but it's not silent & doesn't wake the app.
i need solution for firebase notification & on iOS 10,
i am following same firebase sample code
i got the answer, write payload structure in this way.
{
"data":{
"title":"mytitle",
"body":"mybody",
"url":"myurl"
},
"notification":{
"title":"mytitle",
"body":"mybody",
"content_available": true
},
"to":"DEVICE_FCM_TOKEN"
}
this may help someone.
if you wish to test FCM notification using POSTMAN Api client, use "key=YOUR_SERVER_KEY"

Resources