I have an ios app where i send push using apns. I need to handle push message and if it is correct show the message. How can i realize it in swift? This is my code:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions:
[NSObject: AnyObject]?) -> Bool {
registerForPushNotifications(application)
return true
}
func registerForPushNotifications(application: UIApplication) {
let notificationSettings = UIUserNotificationSettings(
forTypes: [.Badge, .Sound, .Alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types != .None {
application.registerForRemoteNotifications()
}
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
var tokenString = ""
for i in 0..<deviceToken.length {
tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}
print("Device Token:", tokenString)
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Failed to register:", error)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
}
func applicationDidBecomeActive(application: UIApplication) {
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
}
In what function i can handle push notification there?
To handle push notifications when arrived in your iOS application is tricky part in order to take their full advantage.
When push notification arrives, your application can be in
Closed state – App is killed, Running state – App is in Foreground, Suspended state – App is in Background,
Handle push notification when arrived
Lets discuses one by one how to handle them in each state
Closed state:
When Application is closed (some other app is running or phone is locked), push notification arrived and you tapped on it to
open the app. Control will be given to appDelegate’s
method i.e. didFinishLaunchingWithOptions
:
Please note that when you normally launch the app by taping its icon from your phone. didFinishLaunchingWithOptions:
called first with its launchOptions==nil. In case you launch the app by clicking received push notifications didFinishLaunchingWithOptions: called with its launchOptions!=nil
.
Here comes the point If you want to do something special when your application is launched upon clicking push notification you need to add code in your didFinishLaunchingWithOptions
:
like this
if (launchOptions != nil)
{
//do some thing special e.g. display particular ViewController or Set some notification badge value.
}
Running state
If your application is running (in foreground) and push notification received, nothing related to that notification will be displayed on screen – no alert, no message, no sound. Instead following method of appDelegate will be called
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
}
You can implement this method according to your need that how you want to respond to the notification.
Suspended state
If your application is in background (phone is locked or some other app is running) and push notification recieved, notification will be displayed with sound and on clicking that notification app will be launched with following method of appDelegate is called
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
}
Its the same that is called when notification received in Running state. Please note that you can always find if your app is awakened from background state in this method by using UIApplication’s applicationState
property. In this case you can do something special when application is opened from background via push notification.
As per your problem,
You cannot toggle the visibility of the notification once it has been received on the device.
This kind of feature/functionality is only possible on Android. As on receiving the notification, Android devs can decide whether to render the view or not.
So try to control it from the server side.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With