I have iOS app written in Swift. I use Parse SDK for push notifications.
I want to add badge to app icon when push is received. There is problem - I can't add badge from push directly because there are a lot of users that use previous version of my app. And if I add badge from push - this badge will not disappear because previous app versions don't hide badge after it opened. So badge will be always on icon.
So what I want is to handle push by my app. No matter is it running or not. If push arrives - my app adds badge. So I can I handle push by my app if it is not running?
I know how to add badge with Swift
application.applicationIconBadgeNumber = 5
But how can i do it without opening the app - just when push is received?
When you push the app, you need to send the badge count with it:
{
"aps": {
"alert": "message goes here",
"sound": "sound.aiff",
"badge": 5
}
}
This will change the badge number on the app before it has been opened.
Please note that you cannot change the badge number of an app when it is closed without using notifications to do so.
Like said in the Parse docs, you can set badge number setting the badge field in the payload with an integer
ex:
"data": {
"alert": "The Mets scored! The game is now tied 1-1.",
"badge": 5,
"sound": "default",
"title": "Mets Score!"
}
or with Increment to automatically increment current value in badge count
ex:
"data": {
"alert": "The Mets scored! The game is now tied 1-1.",
"badge": "Increment",
"sound": "default",
"title": "Mets Score!"
}
To send push notifications with Parse just use the following code, making sure you change the channel to the correct one:
let push = PFPush()
push.setChannel("MyChannel")
push.sendPushInBackground()
Then to hide the badges once the user opens the app back up, add the following function to your AppDelegate.swift:
func applicationDidBecomeActive(application: UIApplication) {
//Reset badge counter to zero
var currentInstallation = PFInstallation.currentInstallation()
if(currentInstallation.badge != 0){
currentInstallation.badge = 0
}
}
since Parse SDK keeps it's own badge count separate from iOS app badge count, you need to follow it like this to rest the badge count to zero.
- (void)applicationDidBecomeActive:(UIApplication *)application {
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
if (currentInstallation.badge != 0) {
currentInstallation.badge = 0;
[currentInstallation saveEventually];
}
// ...
}
In Swift 2.0,
let parseinst: PFInstallation = PFInstallation.currentInstallation();
if (parseinst.badge != 0)
{
parseinst.badge = 0;
parseinst.saveEventually()
}
Related
I have been looking all over the threads of a way to make the app recieve the silent push notification while the app is terminated.
I have tried:
{
"data" : {
"foo" : boo
},
"apns" : {
"header" :{
'apns-priority' : '10' && i also tried 5
}
"aps" : {
"content-available" : 1
},
}
}
I know it´s possible since ton of apps use it to update app content while app is killed or after mobile restart. When I open the console I can see that the message is arrived but my app doesnt wake up.
I have made a print('xx') right at the beginning of my app.
I have no issue receiving data notifications on background/foreground,
I have a Capacitor/Cordova app using Pushy (https://pushy.me) to handle push notifications. Sending push notifications to devices seems to work fine, and as part of that transaction I can set the app's notification count on the icon when the app is closed.
However, Pushy only seems to have an JS option to clear the counter Pushy.clearBadge(); rather than change it to a specific number from within the app when it's running. The scenario being if a user has read one message, but leaves two unread and then closes the app, I want the counter to be correct.
Digging through the PushyPlugin.swift code, the function for Pushy.clearBadge(); looks like this:
#objc(clearBadge:)
func clearBadge(command: CDVInvokedUrlCommand) {
// Clear app badge
UIApplication.shared.applicationIconBadgeNumber = 0;
// Always success
self.commandDelegate!.send(
CDVPluginResult(
status: CDVCommandStatus_OK
),
callbackId: command.callbackId
)
}
Which is tantalisingly close to giving me what I need, if only I could pass an integer other than zero in that line UIApplication.shared.applicationIconBadgeNumber = 0;
My knowledge of Swift is zilch, other than recognising familiar programming syntax. I thought I'd have a hack at it and tried adding this to the PushyPlugin.swift file:
#objc(setBadge:)
func setBadge(command: CDVInvokedUrlCommand) {
// Clear app badge
UIApplication.shared.applicationIconBadgeNumber = 100;
// Always success
self.commandDelegate!.send(
CDVPluginResult(
status: CDVCommandStatus_OK
),
callbackId: command.callbackId
)
}
But the app coughed up Pushy.setBadge is not a function when I tried to give that a spin (note, I was testing with 100 just to see what would happen, ideally I'd want to pass an integer to that newly hacked in function).
So what I've learnt so far is I know nothing about Swift.
Am I sort of on the right lines with this, or is there a better way to set the badge counter?
Ok, so I spotted in the pushy-cordova/www folder a Pushy.js file which I needed to add my new function to
{
name: 'setBadge',
noError: true,
noCallback: true,
platforms: ['ios']
},
Then, a bit of trial and error with the PushyPlugin.swift function I added to this:
#objc(setBadge:)
func setBadge(command: CDVInvokedUrlCommand) {
UIApplication.shared.applicationIconBadgeNumber = command.arguments[0] as! Int;
// Always success
self.commandDelegate!.send(
CDVPluginResult(
status: CDVCommandStatus_OK
),
callbackId: command.callbackId
)
}
Then this can be called in my Capacitor project with Pushy.setBadge(X); (X being the int value you want to display on your icon).
This has obviously been asked multiple times but I can't seem to get any of the proposed solutions working.
Push notifs work when tethered to xcode. Untethered, I get the push notification itself but none of the delegate methods are called which results in not being able to increment badge count.
I also can't get silent notifications working on untethered as well. I use silent notifications to clear a notification with a given condition.
I've tried allowing background mode in plist, changing priorities, having an empty body. I've made sure that the device background refresh is on and that notifs are fully on. I've tried a ton more things that I can't remember off the top of my head. Nothing seems to work. I added a badge increment in every one of the push notif delegate methods and it seems none of them get called.
Here is the .js code snippet for regular notif
message1 = {
notification: {
title: "stackOverFlowTitle",
body: "stackOverFlowBody"
},
apns: {
payload: {
aps : {
"content-available": 1,
"sound": "default",
"priority" : 10
},
data: {
// collection: 'TestCollection',
// document: change.after.ref.id,
timePushed: timePushed,
alertOn: alertOn,
readNotification: readNotification,
eventKey: eventKey
}
},
headers: {
"apns-expiration": "4"
}
},
topic: topic
};
Here is the .js code snippet for silent notif
var message2 = {
notification: {
body: ""
},
apns: {
payload: {
aps: {
"content-available": 1,
"priority" : 10
},
data: {
alertOn: alertOn,
eventKey: eventKey
}
},
headers: {
"apns-expiration": "4"
}
},
topic: topic
};
Anyone have any potential solutions I haven't mentioned?
I changed silent notif priority to 5 and that helped (for this post) :D
According to the docs, when non-zero the apns-expiration represents a unix timestamp. By setting it to 4, you're telling APNs to expire the notification at 1970-01-01 00:00:04.
Hi I want to implement push notifications , I have three devices to test it : Iphones 4, 5s , 6
All of them using IOS 9.3.2 and are registering in my provisioning profile.
The push notifications work just fine in my Iphone 5s and if my app is in active mode, Im getting the following message in the console when receiving push:
%# [aps: {
alert = {
body = "Jonathan : test me ";
title = test;
};
}, gcm.message_id: 0:1470515051789092%8d989dbf8d989dbf]
but when I try to get manage to get push notification in my other device , Im not getting the push in background and when entering my app Im gettting the following message in my console:
%# [from: 199968158838, collapse_key: com.jerem.***********, notification: {
body = "Jerem :test me ";
e = 1;
title = test;
}]
I dont succeed in sending them notifications from the Firebase console too,
thanks for your help!
I had the same problem with iOS 9.3.2, but when I reinstall my app not working on iPhone 5s too. The problem was with token, so I add this method and is's working:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: .Sandbox)
}
Remember .Sandbox type is for development mode.
I am facing one unusual issue that my notification is not coming in notification tray. Though I am getting sound and begde count too. So there should not be any problem with provisioning profile. I am not able to find anything with googling.
I have tested this case when app is in background and killed.
I have checked in settings also for my application its banner is selected.I also try with alerts but alerts are not appearing.
Any idea about this issue?
NSDictionary *userInfo = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
I am getting this userInfo = nil
Is this the issue notification not appearing in notification bar?
Finally I got the issue and that was from server side.
Might be one of the same case for others too..
aps = {
alert = {
dictioanry = {
key = value;
key = value;
};
};
badge = 1;
sound = default;
};
Here I asked the php person to add body tag and I was able to get notifications in tray..
So for the people with same issue just check out if you are missing any key from the server side.
For those who are using FCM Push Notif, might need to check wheter the payload has "notification" field there.
Here is the example of full payload working with FCM:
{
"registration_ids" : ["xx5MSpbpuUe6lgYhXTX-gs:APA91bFxKmDCTlE2WA_hl29XjiNK7A-qpdRBJaz-JddCsx5CAxxxxxxxxxxGcoO0-moOMqT2T59cExxxxxt-uMgLUQgg4t8jL3uuccka6psdBXVl26bx2HfnSbvKAxxxxxx-x_xxxxx_"],
"priority": "high",
"data" : {
"app_link": "xyz://abc/def/123/456",
"title": "Testing",
"icon": "htt://abc/image.png",
},
"notification": {
"body": "Notification body",
"title": "Here is the title",
"subtitle": "and this is subtitle",
"sound" : true,
"content_available": true,
"mutable_content": true,
"priority": "high",
"badge": "1"
}
}