Push notifications on Swift - ios

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.

Related

Notification delegate not being called when app is in background when notification is received

I am using firebase cloud messaging to send push notifications to our iOS application. When the application is in the foreground everything works fine.
The problem is when i send the app to the background and then send a notification.
I expect the following delegate to be called, but it is not:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult)-> Void) {
I have ticked the "remote notifications" background mode in my apps signing & capabilities tab.
The payload i send to firebase looks like this:
{"to":"--TOKEN--",
"priority":"high",
"content_available":true,
"mutable_content": true,
"notification":
{"body":"Test Notification",
"title":"New Notification",
"sound":"default",
"click_action":"notification"
}
}
This payload is sent to firebase via https://fcm.googleapis.com/fcm/send
When i click an notification it is then processed, and i can see the apns version i receive looks like this:
[AnyHashable("google.c.sender.id"): 8xxxxxxxx, AnyHashable("gcm.message_id"): 1xxxxxxxxxx, AnyHashable("google.c.a.e"): 1, AnyHashable("google.c.fid"): fxxxxxxxxx, AnyHashable("aps"): {
alert = {
body = "Test Notification";
title = "New Notification";
};
category = feedback;
"content-available" = 1;
"mutable-content" = 1;
sound = default;
}]
I am not sure why content-available and mutable-content appear in quotes? I know firebase convers its payload to apns format, is there something wrong here?
I want the delegate to be called so that i can execute some code to maintain various data items, so it is important that i can run some code when my app is in the background and a notification is received.
I am not sure what config i am missing as everything i read seems to say this is all i need to do?

Notification service extension not come to device with api.sandbox.push.apple.com

I have an issue with Notification Service Extension on iOS 13. This issue is not always happen.
When I push a notification with mutable-content = 1, on development apple server ( api.sandbox.push.apple.com:443), Apple return success on my server, but no notification comes to my device. Sometimes it still comes, but very late, for example I push it at 9:00 AM, it comes to my device at 9:15 AM, and show exactly time (15 mins ago).
Is this an apple error or my error? If it's my error, how do I fix this? And if it's Apple's error, does it happen on real server (api.push.apple.com:443)?
Thanks!
"Mutable-content" should be inside "aps" dictionary
Try this if you are sending from apns server
{
"aps": {
"category": "content_added_notification",
"alert": {
"title": "Photos",
"body": "Antoine added something new - take a look"
},
"mutable-content": 1
},
"image_url": "https://www.example.com/image_url"
}
And this if you are sending from firebase
let dict = ["to": "SERVER-KEY",
"notification":["body":"body text",
"title":"notification text",
"category": "myNotificationCategory",
"mutable-content": true],
"data":[
"mediaUrl": "https://homepages.cae.wisc.edu/~ece533/images/airplane.png"
]]as [String:Any]

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"

Push Notification Delivery and Dismiss Callback

Can we make a service call from the client side when a push notification is delivered or dismissed when the app is not active?
Also does APNS give any information on whether the notification is delivered at an individual level and is there any api we can use to query the delivery status report?
Yes you can detect delivery of remote notification if you send the content-available flag to 1 in the aps dictionary. If you set the key, application:didReceiveRemoteNotification:fetchCompletionHandler: method of the AppDelegate is called.
Also, from iOS 10 onwards, you can also detect dismissal of remote notifications. For that you need to implement UserNotifications Framework.
You need to perform the below steps for the same:
Use iOS 10 APIs to register the categories for remote notifications.
You need to check [[UNUserNotificaionCenter currentNotificationCenter] setNotificationCategories] method for the same. See https://developer.apple.com/documentation/usernotifications/unusernotificationcenter
iOS 10 introduces a new class called UNNotificationCategory for encapsulating a category instance. You need to set the CustomDismissAction on the category instance to be able to receive the dismiss callback.
See https://developer.apple.com/documentation/usernotifications/unnotificationcategory
Implement the UNUserNotificationCenterDelegate protocol with the implementation of userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: method. This method will receive the callback to the dismiss action provided you perform the above steps.
See https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate
Set your delegate to the UserNotificationCenter - [[UNUserNotificaionCenter currentNotificationCenter].delegate = your_delegate_implementation
Set the category with CustomDismissAction property in the payload.
{
"aps": {
"alert": "joetheman",
"sound": "default",
"category": "my-custom-dismiss-action-category",
"content-available":1
},
"message": "Some custom message for your app",
"id": 1234
}
Yes you can make a service call from the ios app when a notification is received when app is not active. Do this in:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
<#code#>
}
yes you can add additional key value pair in your push notification payload.
{
"aps": {
"alert": "joetheman",
"sound": "default"
},
"message": "Some custom message for your app",
"id": 1234
}

Resources