How to debug background notifications in iOS? - 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.

Related

iOS 10 FireBase when notification arrived, do not get call NotificationService.h Notification Service Extension

I use firebase and get successfully Notifications but when notification arrived, do not get call NotificationService.h Notification Service Extension.
I also set
NSExtensionPrincipalClass to NotificationService
NSExtensionPointIdentifier to com.apple.usernotifications.service
My notification is like
{
"registration_ids": ["devicetoken"],
"mutable_content": true,
"data":{
"post_details": {
"pcm_message": "asdf",
"pcm_img_url": "http://portalvhds34w6bf5z9b21h.blob.core.windows.net/images/1519365008_5a8fab90cf683.jpg",
}
},
"notification" : {
"title" : "demo push",
"body" : "this is push body" }
}
where is a problem or missing some information I have already set deployment target 10.0 in my whole project.

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"

How to send a silent Push Notification payload

I just want to know how I can determine what action to do on a silent push:
This is the aps that I sent to the client:
"aps": {
"content-available": 1
}
My problem now is when I add type: "Order_Update" to determine that the silent push is for the Order Update to display an alert notification.
There are a few options for it! Let's take a small ride to understand all the different payloads and their usage.
Simple Payload
Displayed in Notification Center : Yes
Wakes app to perform background task : No
{
"aps" : {
"alert" : "You received simple notification!",
"badge" : 1,
"sound" : "default"
}
}
Payload With Custom Notification Sound
Displayed in Notification Center : Yes
Wakes app to perform background task : No
Step 1 : Add custom notification sound file (.wav or .aiff extensions only. e.g. notification.wav) in your app bundle.
Step 2 : Configure your payload as shown below to play your custom sound
{
"aps" : {
"alert" : "It's a custom notification sound!",
"badge" : 1,
"sound" : "notification.wav"
}
}
Notification With Custom Payload
Displayed in Notification Center : Yes
Wakes app to perform background task : No
{
"aps" : {
"alert" : "It's a notification with custom payload!",
"badge" : 1,
"content-available" : 0
},
"data" :{
"title" : "Game Request",
"body" : "Bob wants to play poker",
"action-loc-key" : "PLAY"
},
}
Here the data dictionary holds custom information whatever you want. It will also display as normal notification with the alert message "It's a notification with custom payload!".
Normal Silent Notification
It will not a show an alert as a notification bar; it will only notify your app that there is some new data available, prompting the app to fetch new content.
Displayed in Notification center : No
Awake app to perform background task : Yes
{
"content-available" : 1
}
Silent Notification With Custom Payload
Here comes the magic to show a notification alert as well awake your app in background for a task! (Note: only if it's running in background and has not been killed explicitly by the user.)
Just add the extra parameter "content-available" : 1 in your payload.
Displayed in Notification Center : Yes
Wakes app to perform background task : Yes
{
"aps" : {
"alert" : "Notification with custom payload!",
"badge" : 1,
"content-available" : 1
},
"data" :{
"title" : "Game Request",
"body" : "Bob wants to play poker",
"action-loc-key" : "PLAY"
}
}
Use any of these payloads according to your app requirements. For background app refresh refer to Apple's documentation. I hope this gives you all the necessary information. Happy coding :)
As i understand, you want extra data inside payload, so you can identify what push notification type is,or what action need to be handled.
For that edit your payload as:
$body = array(
'content-available' => 1,
'sound' => ''
);
$payload = array();
$payload['aps'] = $body;
$payload['action'] = 'order_update';
Then in your iOS Code:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *action = userInfo["action"];
if([userInfo[#"aps"][#"content-available"] intValue]== 1 && [action isEqualToString:#"order_update") //order update notification
{
//handle Your Action here
return;
}
}
Hope this solves your problem!
Please also check apns-push-type (Required for watchOS 6 and later; recommended for macOS, iOS, tvOS, and iPadOS) The value of this header must accurately reflect the contents of your notification’s payload. If there is a mismatch, or if the header is missing on required systems, APNs may return an error, delay the delivery of the notification, or drop it altogether.
https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns

GCM downstream messaging JSON format for 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.

Resources