I am working on a project where I want to make Push notifications work, on Parse-Server (Heroku), with an iOS app.
This is the code I use on the server side to generate a PUSH:
const pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo('deviceType', 'ios');
Parse.Push.send({
where: pushQuery, // Set our Installation query
data: {alert: "ABCXYZ"}
}, {
useMasterKey: true,
success: function() {},
error: function(error) {
throw "Got an error " + error.code + " : " + error.message;
}
});
On the iOS app side, I receive the notification, but I would like to tweak it to my taste, if possible.
Here is the relevant swift code where I can see the notification coming:
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
print(#function)
print("NN = \(notification.description)")
}
And finally, this is what I can see in the Xcode debugging console, when the notification arrives:
userNotificationCenter(_:willPresent:withCompletionHandler:)
NN = <UNNotification: 0x2....; date: 2020-03-02 06:51:39 +0000,
request: <UNNotificationRequest: 0x28....3e0; identifier: 0C....AF3,
content: <UNNotificationContent: 0x28....6c0; title: (null), subtitle: (null),
body: ABCXYZ, summaryArgument: , summaryArgumentCount: 0, categoryIdentifier: ,
launchImageName: , threadIdentifier: , attachments: (
), badge: (null), sound: (null),,
trigger: <UNPushNotificationTrigger: 0x28...0;
contentAvailable: NO, mutableContent: NO>>>
Obviously, it is working up to a point. But I can see fields in the incoming notification that I do not control. Namely: title, subtitle, summaryArgument, categoryIdentifier ...... In fact only "body" is what I have set on the server side.
Therefore my question is: how can I set all those fields the way I wish.
I have already tried something like this:
data: {
title: "MyOwnTitle",
alert: "ABCXYZ"
}
But without success.
Furthermore, by using something like:
data: {
alert: "ABCXYZ",
content_available: 1,
push_type: "background",
category: "S3x"
}
I can see the following in the Xcode debugging console, when the notification arrives:
userNotificationCenter(_:willPresent:withCompletionHandler:)
NN = <UNNotification: 0x28...; date: 2020-03-03 ...,
request: <UNNotificationRequest: 0x2..;
identifier: BF...EE, content: <UNNotificationContent: 0x28..;
title: (null), subtitle: (null), body: ABCXYZ,
summaryArgument: , summaryArgumentCount: 0,
categoryIdentifier: S3x, launchImageName: ,
threadIdentifier: , attachments: (
), badge: (null), sound: (null),,
trigger: <UNPushNotificationTrigger: 0x28..;
contentAvailable: NO, mutableContent: NO>>>
Where it appears that the "ABCXYZ" part is transferred as well as the category (S3x), but the rest (content_available,push_type) seems to be ignored.
According to Parse push notification docs, you can send the following options in your push notification payload:
alert: the notification’s message.
badge: (iOS only) the value indicated in the top right corner of the app icon. This can be set to a value or to Increment in order to increment the current value by 1.
sound: (iOS only) the name of a sound file in the application bundle.
content-available: (iOS only) If you are a writing an app using the Remote Notification Background Mode introduced in iOS7 (a.k.a. “Background Push”), set this value to 1 to trigger a background download. You also have to set push_type starting iOS 13 and watchOS 6.
push_type: (iOS only) The type of the notification. The value is alert or background. Specify alert when the delivery of your notification displays an alert, plays a sound, or badges your app’s icon. Specify background for silent notifications that do not interact with the user. Defaults to alert if no value is set. Required when delivering notifications to devices running iOS 13 and later, or watchOS 6 and later.
priority: (iOS only) The priority of the notification. Specify 10 to send the notification immediately. Specify 5 to send the notification based on power considerations on the user’s device. (More detailed documentation)
category: (iOS only) the identifier of the UNNotificationCategory for this push notification.
uri: (Android only) an optional field that contains a URI. When the notification is opened, an Activity associated with opening the URI is launched.
title: (Android only) the value displayed in the Android system tray notification.
Besides these, you can also write a custom push notification handler in your iOS app by following this guide: http://docs.parseplatform.org/ios/guide/#responding-to-the-payload
Related
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.
Does someone have experience with ionic 1 push notifications? Is there any alternative to cloud solution that they suggest? And can someone show example of implementatio? And please have on mind that I'm completly new in ionic
I use this plugin https://github.com/phonegap/phonegap-plugin-push to handle push notification.
var pushOptions = {
android: {
senderID: "1234567890",
icon: "push_icon",
iconColor: "#FFF"
},
ios: {
alert: true,
badge: true,
sound: true
}
};
var push = PushNotification.init(pushOptions);
push.on('registration', function () {});
push.on('notification', function () {});
After test several push servicies (ionic push service, parse, pushwoosh and one signal) we choose one signal as push notification provider. Right now we use it in 30+ production applications in our company. In 10 minutes or less you can set up an app for first push notification use in android or iOS (platforms we are working right now)
The platform has an easy set up guide.
The only code in app for basic setup is:
$ionicPlatform.ready(function() {
// Enable to debug issues.
// window.plugins.OneSignal.setLogLevel({logLevel: 4, visualLevel: 4});
var notificationOpenedCallback = function(jsonData) {
console.log('notificationOpenedCallback: ' + JSON.stringify(jsonData));
};
window.plugins.OneSignal
.startInit("YOUR_APPID")
.handleNotificationOpened(notificationOpenedCallback)
.endInit();
// Call syncHashedEmail anywhere in your app if you have the user's email.
// This improves the effectiveness of OneSignal's "best-time" notification scheduling feature.
// window.plugins.OneSignal.syncHashedEmail(userEmail);
})
Just modify "YOUR_APPID" by yours and thats all.
Follow de docs above for make it works in your desired platform.
Check out OneSignal. It's free, easy to use and multi-platform Push Notification framework.
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 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()
}