How can I get the push notification text when the app does not run or in lock screen?
I did try:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
println(userInfo)
// or just
println("something")
}
But it doesn't print me anything. Is it wrong function???
didReceiveRemoteNotification function only called this condition.
while app will running and app in foreground.(alert not showing)
if app in background and click notification on home screen
if app not run in device didReceiveRemoteNotification function not called,
in this case user click notification in home screen, we identify app launched by notification or not in didFinishLaunchingWithOptions
UILocalNotification *localNotif =[launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
you get userinfo data in localNotif.userinfo
Try in Swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var userinfo : NSDictionary =launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
print(launchOptions);
return true
}
Related
IOS 13.7 IPhone Xr, Xcode Version 11.7 (11E801a)
I edited app scheme and set launch mode to "Wait for the executable to be launched". I run app, and send a simple notification on my device.
this notification:
{
"aps":{
"content-available":1
}
}
in xcode status changed from "Waiting to attach to test on iPhone" to "Running test on iPhone", but the notification wasn't received.
i try catch notification in this method:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void)
Next notification I receive successfully. This case can be repeated after application reload.
Can you help me find out why I don't receive the first notification
Please try this payload :
{"aps":{"alert":"","content-available":1}}
If your app wasn’t running, the push notification is passed to your AppDelegate in the launchOptions
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
...
let notificationOption = launchOptions?[.remoteNotification]
if let notification = notificationOption as? [String:AnyObject]{
// received notification
}
...
}
I have set up the silent push notification for my app:
1. I configured the push notification from all places, i.e., XCode, Apple Developer portal with proper certificate
2. I enabled background capability
3. I included "content-available" in the json payload.
However, my App can not receive silent push sometimes.
To be more specific, neither
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
......
}
nor
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (_: UIBackgroundFetchResult) -> Void) {
......
}
has been invoked when the push message arrives.
How can I get the information in the push notification in such state?
I'm waiting online.
You question is twofold:
1. why the two delegate callbacks can not be invoked
2. what should you do to receive the data.
I did some hand test and here is the result:
1, when an app is in killed state, the two callback can not be invoked indeed
2. however, when you open the app next time,
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
......
}
can be invoked and you can get the data from there.
I hope this is useful.
I have a Swift 2 iOS8+ app where I need to make a request to fetch JSON data when my app receives a push notification.
When the user clicks on the notification the app will go and fetch the data but I really need the data to be fetched as soon as the notification is received. This looks to be possible, is this the case?
I've implemented:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)){
and checking the launch options in:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
I've also enabled:
background fetch
remote notifications
None of this seems to help. If this is possible I'd be grateful for any pointers/tutorials on this.
As of iOS 9, instead of a normal push, you can use silent push notifications. When your app receives a silent push, the user is not notified, but your app can perform actions based on this notification. Then, when your background actions is finished, you create a local notification for the user.
Check out this tutorial for info on how to use silent notifications:
https://www.raywenderlich.com/123862/push-notifications-tutorial
I am trying to read the text in the notification, so I can assign labels values based on what the notification says. And I also need help on opening a certain view controller when the notification is clicked. How would I do this? I am very new to dealing with notifications, so any help is appreciated.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let launchOptions = launchOptions as? [String : AnyObject] {
if let notificationDictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {
self.application(application, didReceiveRemoteNotification: notificationDictionary)
print("text: \(notificationDictionary)")
}
}
return true
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
PFPush.handlePush(userInfo)
}
You can get the text of the notification with
userInfo["aps"]!["alert"]
didReceiveRemoteNotification is called
when the notif is received if the app is in the foreground
when the notif is clicked if the app was in the background
To distinguish between these cases :
if application.applicationState == UIApplicationState.Active { }
Note that there is another case you need to handle: the notif is clicked while the app is closed. In this case, didReceiveRemoteNotification is not called, but the notif will be accessible in the didFinishLaunchingWithOptions launch options :
launchOptions![UIApplicationLaunchOptionsRemoteNotificationKey]
How do I handle a push remote notification when the app is not running. Currently, when remote notification comes in, and my app is in foreground or background, my code is correctly handling the remote notification. But when the app is not running, and remote notification come in, I see a remote banner on top of screen, and when I tapped on it, my app get launch and go away. It doesn't seem to handle the remote notification correctly. Is this how iOS behave, or is there a way to handle it. Thanks. This is currently my code in AppDelegate.swift.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
...
println("..... application didFinishLaunchingWithOptions()")
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
// there is a notification...do stuff...
println("dinFInishLaunchingWithOption().. calling didREceiveRemoteNotification")
self.application(application, didReceiveRemoteNotification: remoteNotification as [NSObject : AnyObject])
}
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
{
println("didRegisterForRemoteNotificationsWithDeviceToken")
}
fun application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError)
{
println(error)
println(error.localizedDescription)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
{
println("..... application didReceiveRemoteNotification()")
}