Device Token not received - ios

iPhone device is not receiving device token from my application. The
didRegisterForRemoteNotificationsWithDeviceToken
method is not getting called though I have the following code in the
didFinishLaunch
method.
In didFinishLaunch Method:
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
Note:
notificationSettings method is getting called. but not didRegister or didFail methods called for Remote Notifications.
I am using a iPhone 6s with iOS 9.
What need to be checked to get the device token?

You are using UNUserNotificationCenter but it only work if your device with iOS 10 or 10+.
For iOS 9 you need below code for register push notification
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
You can add condition based on iOS version like #Kamalesh answer.

try this in didFinishLaunch
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
} else {
// Fallback on earlier versions
}
// ios 10
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound,.badge]) { (granted, error) in
// actions based on whether notifications were authorized or not
}
UIApplication.shared.registerForRemoteNotifications()
}
// iOS 9 support
else if #available(iOS 9, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
else
{
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
}
}

Related

Allowing Push Notifications Version Control

Swift 3 IOS 10: I've been looking for code for allowing pushing notification; finally, I could find a precisely useful one from www.codementor.io. However, I came into puzzle. I wonder if the following code will work with the lower or newer version of iOS. Since Apple will be releasing its version relentlessly, how the following code will be able to handle the changes? Is there any way out to deal with the problem I mentioned?
// iOS 10 support
if #available(iOS 10, *) {
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
application.registerForRemoteNotifications()
}
// iOS 9 support
else if #available(iOS 9, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
// iOS 8 support
else if #available(iOS 8, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
// iOS 7 support
else {
application.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
}
Yes the code for iOS 10 will work for iOS 11 as well.
Basically the #available macro checks the minimum OS so it's also safe to merge iOS 8 and 9.
//iOS 10+
if #available(iOS 10, *) {
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in
if (granted) { //check if authorization was granted before registering
application.registerForRemoteNotifications()
}
}
}
// iOS 8+ support
else if #available(iOS 8, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
// iOS 7 support
else {
application.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
}
Try this:
func setUpPushNotification(application: UIApplication) {
if #available(iOS 10.0, *) {
let notificationTypes: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: notificationTypes,
completionHandler: {_,_ in })
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
else{
let notificationTypes: UIUserNotificationType = [.alert, .badge, .sound]
let pushNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil)
application.registerUserNotificationSettings(pushNotificationSettings)
}
application.registerForRemoteNotifications()
}
if #available(iOS 10, *)
reads as: if iOS from Version 10 or above is available...
So if you leave your code like that and iOS 11, 12, etc is coming along they will all go into this if branch.
So your code will work as long as the Push API is not changed.
If Apple changes the API in the future (say in iOS 12) you will have to add another if-branch and re-submit you app to the store...
(If someone with iOS 9 uses your app they will end up in the second if branch - because the first one does not apply. So the order of the ifs guarantees that lower iOS version get the right code.)

In my app latest notification is replacing previous one, how to stop this behavior?

In my app latest notification is replacing previous one, how to stop this behavior show that all notifications are shown in notification center until user clears.
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
//center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
}
} else {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}

If my app is open or in background i got the notifications. But if i force closed to my app i don't get the notification

How to show the notification to user even app is not running or force closed by user.
If my app is open or in background i got the notifications. But if i force closed to my app i don't get the notification in xcode 8.3 and swift 3 iOS
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
//center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
}
}
else {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
In this way i am registering for notifications

Does Firebase 3.7.0 support in iOS 7 and 8

I am using Firebase in my app. I have installed via the Cocoapods. To make support for push notification in iOS 10 I have updated the cocoa pods with the latest version of Firebase 3.7.0.
Please let me know does it support in iOS 7 and iOS 8.
Registeration of push notification like this
if #available(iOS 10.0, *) {
let authOptions : UNAuthorizationOptions = [.Alert, .Badge, .Sound]
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
authOptions,
completionHandler: {_,_ in })
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.currentNotificationCenter().delegate = self
// For iOS 10 data message (sent via FCM)
FIRMessaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}
Thanks

How to register Notification directly for Alert and not just Banner? Swift or Objective C in XCode

That's how i register my local notifications:
if application.respondsToSelector(#selector(UIApplication.registerUserNotificationSettings(_:))) {
if #available(iOS 8.0, *) {
let types:UIUserNotificationType = ([.Alert, .Sound, .Badge])
let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categoriesForSettings as! Set<UIUserNotificationCategory>)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
} else {
application.registerForRemoteNotificationTypes([.Alert, .Sound, .Badge])
}
}
else
{
// Register for Push Notifications before iOS 8
application.registerForRemoteNotificationTypes([.Alert, .Sound, .Badge])
}
My Problem is that the notification comes in as a Banner. Only after the user changes the settings of the application from Banner to Alert the Notification comes in as a real Alert.
My Question is: Is it possible to ask or register directly for the real Alert and not only the Banner notification?
Thanks in Advance!
Sadly it can't be changed from code.

Resources