Is there any reason why a push notification would appear on the lock screen and then instantly disappear? I am using the didReceiveRemoteNotification block to save data in the notification.
Is it possible that your completionHandler code is setting the number of notifications to 0 ? That would make it disappear.
Either you put the number of notifications to 0 in your code like
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
Or in you Notification payload, you have a the badge property set to 0
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 0,
"sound" : "bingbong.aiff"
},
}
Related
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.
Within AppDelegate I simply update applicationIconBadgeNumber:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
application.applicationIconBadgeNumber += 1
}
Everything works as expected when the app is connected to Xcode and is in debugger mode. But just after I plug it out from Xcode, notification arrives but badge is not updated. App is in background mode.
Why? What is wrong with my approach? Please, give me an advice🏆
Push notification are handled by iOS and not your app you can't change the application badge on receiving a push notification.
You can send the badge number in the payload of the push notification,
Payload could look like this:
{
"aps" : {
"alert" : "Notification REceived",
"badge" : 1
}
}
I found this post very helpful for managing notifications in the background, basically you have to use the call with completion handler.
didReceiveRemoteNotification when in background
I think the best way to manage badge count is on the server.
A push notification will automatically update the badge on the app icon.
Payload formate.
{
"aps" : {
"alert" : "Weather report udapted",
"badge" : 5
}
}
Your messagingManager is an optional. You should check, that it isn't nil. If it is nil, the appDidReciveMessage() function would not be triggered
I integrated the Push Notification to the CloudKit so that every change in the iCloud will pop up a notification on my iPhone and the badge app icon number will add one correspondingly. However, when I used the code:
application.applicationIconBadgeNumber = 0
to reset that number in the applicationDidBecomeActive(_ application: UIApplication), I noticed that the badge app icon number truly disappeared but if another new notification came again, the number won't start from one again as supposed but just add one to the original total number before the reset. Therefore the number is getting bigger and bigger. I wonder how to solve this problem?
The problem is your apns payload, it contains badge count more than 1, you need to reset the payload as well.
When you set application.applicationIconBadgeNumber = 0 it just resets the badge count locally, not in the server.
Solution would be reset the badge count for the user in the server too.
Update: Apns Payload
{
"aps" : {
"alert" : {
"title" : "Push",
"body" : "Hello User"
},
"badge" : 5
}
}
The app shows the badge count same as in the apns payload above, you need to reset the badge value in the payload above from server.
Hope it helps.
Cheers.
I find that I should not only set the application side like:
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
but I should also set the iCloud side in CKContainer. Therefore, the complete code is like below:
let operation = CKModifyBadgeOperation(badgeValue: 0)
operation.modifyBadgeCompletionBlock = {(error) in
if let error = error{
print("\(error)")
return
}
application.applicationIconBadgeNumber = 0
}
CKContainer.default().add(operation)
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
I am using Push Notifications in my app.when notifications comes,i am not getting the notification sound of the notification and also in iPad settings i switched-on all the notification buttons in iPad even though the notification sound is not coming.
{
alert = "Testing commands#2014-12-01T12:16:26",
sound = "default"
}
I am getting the sound file like this.But the notification sound is not coming.
I am new to the PushNotification concept.Can anyone please help to solve this...
you should use JSON format to send your notifications encapsulated in aps dictionary like:
{
"aps" : {
"alert" : "Your message.",
"badge" : 0,
"sound" : "default"
}
}
for complete reference:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html