UILocalNotification didn't showed up - ios

This is my ReceiveRemoteNotification code.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
}
let notifiAlert = UIAlertView()
let aps = userInfo["aps"] as? [NSObject: AnyObject]
let alert1 = aps!["alerts"] as? String
let link1 = aps!["links"] as? String
notifiAlert.title = alert1!
notifiAlert.message = link1
notifiAlert.addButtonWithTitle("OK")
notifiAlert.show()
print(userInfo)
print("success")
}
i tried to receive JSON data from my web, my JSON looks like :
{"aps":{"alerts":"3","links":"","sounds":"default"}}
I did receive the Json data and turn the data to the view. with this code :
let aps = userInfo["aps"] as? [NSObject: AnyObject]
let alert1 = aps!["alerts"] as? String
let link1 = aps!["links"] as? String
notifiAlert.title = alert1!
notifiAlert.message = link1
notifiAlert.addButtonWithTitle("OK")
notifiAlert.show()
print(userInfo)
print("success")
}
However i only receive when i was using the application, but if i'm not using, i can't receive the notification. but my phone was vibrate or make sound, but no message.
Am i missing something right here ? thanks !
Added Here is my full code http://pastebin.com/mkFiq6Cs
EDIT
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
}
let aps = userInfo["aps"] as? [NSObject: AnyObject]
let alert1 = aps!["alerts"] as? String
let link1 = aps!["links"] as? String
notifiAlert.title = alert1!
notifiAlert.message = link1
notifiAlert.addButtonWithTitle("OK")
notifiAlert.show()
print(userInfo)
print("success")
}
So i have to add this code :
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
if let userInfo = notification.userInfo {
let aps = userInfo["aps"] as? [NSObject: AnyObject]
let alert2 = aps!["alerts"] as? String
let link2 = aps!["links"] as? String
print("didReceiveLocalNotification: \(aps)")
print("didReceiveLocalNotification: \(alert2)")
print("didReceiveLocalNotification: \(link2)")
}
}
And this at didFinishLaunchingOption :
if let options = launchOptions {
if let notification = options[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {
if let userInfo = notification.userInfo {
let customField1 = userInfo["CustomField1"] as! String
// do something neat here
}
}
}
SOLVED
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
}
let notifiAlert = UIAlertView()
if let aps = userInfo["aps"] as? [NSObject: AnyObject],
let alert1 = aps["alert"] as? String,
let content1 = aps["content-available"] as? String,
let link1 = userInfo["links"] as? String {
notifiAlert.title = alert1
notifiAlert.message = link1
notifiAlert.addButtonWithTitle("OK")
notifiAlert.show()
}
print(userInfo)
print("success")
completionHandler(UIBackgroundFetchResult.NewData)
}
This works perfectly. ! Thanks to #tskulbru

This is because application(_:didReceiveRemoteNotification:) is only called when the application is active. It cant handle app-inactive/background remote notifications.
If the app is not running when a remote notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that remote notification. Instead, your implementation of the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method needs to get the remote notification payload data and respond appropriately.
Source: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html?hl=ar#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification:
As said above, application:didReceiveRemoteNotification needs to have additional implementation if it should be able to handle background notifications.
If you instead want just one method to handle all the goodies for you, and don't have a bunch of different methods to implement basically the same thing, Apple has provided a method called application:didReceiveRemoteNotification:fetchCompletionHandler: to handle all this for you.
You need to implement the application:didReceiveRemoteNotification:fetchCompletionHandler: method instead of this one to handle background notifications. This handles background tasks, and you can then call the completionHandler with the appropiate enum when you are finished (.NoData / .NewData / .Failed).
If you want to have a local notification presented when you receive a remote notification while the application is active you will need to create that in the aforementioned method. And if you want to display an alertview if the application is active, you also need to handle the two scenarios in that method. My recommendation is to switch to application:didReceiveRemoteNotification:fetchCompletionHandler: instead and implement everything there.
So in your case, I would do something like this:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
} else {
let notifiAlert = UIAlertView()
if let aps = userInfo["aps"] as? [NSObject: AnyObject],
let alert1 = aps["alerts"] as? String,
let link1 = aps["links"] as? String {
notifiAlert.title = alert1
notifiAlert.message = link1
notifiAlert.addButtonWithTitle("OK")
notifiAlert.show()
}
print(userInfo)
print("success")
}
completionHandler(UIBackgroundFetchResult.NewData)
}
I've not tested the code but it should be something like that without too many changes. I havent used Parse so I don't know exactly what PFPush.handlePush(userInfo) does, you need to check the documentation for that.
Looks like your apns payload is wrong too. See https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1
In your case:
{"aps":{"alert":"alert content","sound":"default", "content-available": 1}, "links":""}
You had plural form instead of single form. Look at the url I gave for the allowed keys. The links key doesn't exist in aps dictionary, which mean its a custom key and needs to be placed outside the dictionary.
Note that this is the standard push notification way and might not be correct when using Parse.

Related

How to get the topic out of a Firebase message notification in iOS?

In Android you can use getFrom() to read the topic from the notification.
Can anybody help me how I can get to know this in iOS?
The getFrom() function in Android has no counterpart for iOS.
As a workaround, you can add in a custom key-value pair for the name of the topic you are sending the message to in your data payload.
This is also the suggested workaround mentioned in this answer.
In Appdelegate. Implement below function and you can get it in userInfo.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// Access userInfo
let aps = userInfo["aps"] as? NSDictionary
if let aps = aps {
let alert = aps["alert"] as! NSDictionary
let body = alert["body"] as! String
let title = alert["title"] as! String
}
}

How can I get the json string value from a Push Notification?

I need to store the last alert json string value coming from a Push Notification.
Using "aps: \(userInfo["aps"]!)" returns the whole json:
aps: {
alert = "last alert message";
}
but I only need "last alert message".
This is my code:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
dUserInfo = userInfo
print("aps: \(userInfo["aps"]!)")
NSUserDefaults.standardUserDefaults().setObject(dUserInfo, forKey: "last_push")
NSUserDefaults.standardUserDefaults().synchronize()
}
How can I get the message properly?
Try this:
if let aps = userInfo["aps"] as? [String: AnyObject] {
if let alert = aps["alert"] as? String {
NSUserDefaults.standardUserDefaults().setObject(alert, forKey: "last_push")
}
}

Why the didReceiveRemoteNotification is not being called?

I'm trying to create a subscription using CloudKit to fires every time a record is created. I have successfully created the subscription, I can see it in dashboard, but I've noticed that the print I have in didReceiveRemoteNotification was never printed!
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print("oioioioioi")
let viewController: CurrentEventController = self.window?.rootViewController as! CurrentEventController
if let userInfo = userInfo as? [String: NSObject] {
let notification: CKNotification = CKNotification(fromRemoteNotificationDictionary: userInfo)
if (notification.notificationType == CKNotificationType.Query) {
let queryNotification = notification as! CKQueryNotification
let recordID = queryNotification.recordID
viewController.fetchRecord(recordID!)
}
}
}
Thanks for any help!

Receive Custom Notification iOS swift 2.0

I tried to receive my custom notification from my web through parse website.
Code didReceiveRemoteNotification :
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)}
Here is the JSON i received from parse website :
[aps: {
alert = "test adirax";
sound = default; }]
It works well for me and the notification shows up. However when i tried to push data from my website, the notification can't shows/ pop up.
Here is my JSON looks like :
{"aps": {"alerts" : "test",
"links": "",
"sounds": "default"}}
I tried to print(userInfo) and the result is i get all the data like above, but there is no notif.
I guess I'm missing some code to convert the data ?
INFORMATION
For the specific information, i tried receive notification from parse.com through subscribe "channels". so it's not a local notification or else.
ADD Code Added (according to my JSON type)
if let launchOptions = launchOptions {
let userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject: AnyObject]
let aps = userInfo!["aps"] as? [NSObject: AnyObject]
let alert1 = aps!["alerts"] as? String
let link1 = aps!["links"] as? String
}
And this :
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
}
let aps = userInfo["aps"] as? [NSObject: AnyObject]
let alert1 = aps!["alerts"] as? String
let link1 = aps!["links"] as? String
print(userInfo)
print("success")
}
When i debugged one by one, its success i collect all the data, however i still missing the notification showed up ?
SOLVED PART 1
So far i managed to get the data and push the notification out, but it's only when i open the application. Code :
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
}
let notifiAlert = UIAlertView()
let aps = userInfo["aps"] as? [NSObject: AnyObject]
let alert1 = aps!["alerts"] as? String
let link1 = aps!["links"] as? String
notifiAlert.title = alert1!
notifiAlert.message = link1
notifiAlert.addButtonWithTitle("OK")
notifiAlert.show()
print(userInfo)
print("success")
}
I use a local notification trick, but how to pop up the notification when i'm not using the app ?
You have to add custom informations like this:
{"aps": {"alerts" : "test",
"sound": "default"},
"name": "Steven",
"age": "32"
}
And parse it like this:
let aps = userInfo["aps"] as? [NSObject: AnyObject]
let msg = aps!["alert"] as? String
let name = userInfo["name"] as? String
print(name)
EDIT:
You have to check push notification on two functions of UIApplicationDelegate
First.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// check for push message
if let launchOptions = launchOptions {
let userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject: AnyObject]
let aps = userInfo["aps"] as? [NSObject: AnyObject]
let msg = aps!["alert"] as? String
let name = userInfo["name"] as? String
print(name)
}
}
Second.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
let aps = userInfo["aps"] as? [NSObject: AnyObject]
let msg = aps!["alert"] as? String
let name = userInfo["name"] as? String
print(name)
}
You should add your custom data to the top-level of your payload, not to the aps object.
Like this, for instance:
{
"aps": {
"alerts" : "test",
"sounds": "default"
},
"links": "",
}

Direct link to browser with Notification

This is a simple logic, but i can't figure it out.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
}
if let aps = userInfo["aps"] as? [NSObject: AnyObject],
let alert1 = aps["alert"] as? String,
let content1 = aps["content-available"] as? String,
let link1 = aps["links"] as? String {
if link1 == "" {
//Ok pressed do nothing
}else{
// after OK pressed. jump to page "example" http://www.google.com
}
}
completionHandler(UIBackgroundFetchResult.NewData)
}
I have a push from my web. It contain 2 type of push :
I push notify without link
I push with link inside. (if i press the notify ill jump to
the page example : http://www.google.com)
if without link, "link1" will be empty and just showed up message. after that nothing.
However if i push with a link, How can i jump to the browser after i pressed the ok button ?
Added Full code if you want to see http://pastebin.com/cFYqLqs7
ASK Should i use Deep linking in this case ? because the link can change depend on what i push.

Resources