Checking Push Notification Registration: Isregisteredforremotenotifications Not Updating

Checking Push Notification Registration: isRegisteredForRemoteNotifications Not Updating

Because iOS 8 does register the device and provides a Token even if the user opts out from pushes.

In that case pushes are not presented to the user when the push is sent, but if your app is running it gets the payload so you can update it when the app is running...

To check if push notifications are enabled in iOS 8 you should check for the enabled user notification types:

- (BOOL)pushNotificationsEnabled {
if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {
UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
return (types & UIUserNotificationTypeAlert);
}
else {
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
return (types & UIRemoteNotificationTypeAlert);
}
}

isRegisteredForRemoteNotifications returns false even registration succeed

I solved this by updating all related deprecated methods.

isRegisteredForRemoteNotifications returns true even though I disabled it completely

As per the Apple documentation isRegisteredForRemoteNotifications will return NO if registration has not occurred, has failed, or has been denied by the user. YES will be returned if the app has registered for remote notifications and has received is device token. So in answer to your question NO it will not always return no it will also return yes if a your app has registered for remote notifications and it has received it device token.

Return Value YES if the app is registered for remote notifications and
received its device token or NO if registration has not occurred, has
failed, or has been denied by the user.

Discussion This method reflects only the successful completion of the
remote registration process that begins when you call the
registerForRemoteNotifications method. This method does not reflect
whether remote notifications are actually available due to
connectivity issues. The value returned by this method takes into
account the user’s preferences for receiving remote notifications.

Above point return into apple document.

-----EDITED----------

You can read your app's permissions using

UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; 

Then performing a any operation with the different types to see which are enabled. You can also call unregisterForRemoteNotifications to disable notifications.

-----Changes------

- isRegisteredForRemoteNotifications

Returns a Boolean indicating whether the app is currently registered
for remote notifications.

Declaration SWIFT func isRegisteredForRemoteNotifications() -> Bool


OBJECTIVE-C
- (BOOL)isRegisteredForRemoteNotifications


Return Value YES if the app is registered for remote notifications and
received its device token or NO if registration has not occurred, has
failed, or has been denied by the user.

Discussion This method reflects only the successful completion of the
remote registration process that begins when you call the
registerForRemoteNotifications method. This method does not reflect
whether remote notifications are actually available due to
connectivity issues. The value returned by this method takes into
account the user’s preferences for receiving remote notifications.

UIApplication.shared.isRegisteredForRemoteNotifications always return true

What I understood is that if "isRegistered" is false you will ask the user to allow notifications in that case you should be asking for authorization like this :

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
if granted {
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})
}
}

Device Token not received when registering for remote notifications in Swift

You can try this

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

var types: UIUserNotificationType = UIUserNotificationType.Badge |
UIUserNotificationType.Alert |
UIUserNotificationType.Sound

var settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)

application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()

return true
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

var characterSet: NSCharacterSet = NSCharacterSet(charactersInString: "<>")

var deviceTokenString: String = (deviceToken.description as NSString)
.stringByTrimmingCharactersInSet(characterSet)
.stringByReplacingOccurrencesOfString( " ", withString: "") as String

println(deviceTokenString)

}

EDIT: Update for Swift 2.x

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)

application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()

return true
}

EDIT: Update for Swift 3.x

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)

application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()

return true
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let characterSet = CharacterSet(charactersIn: "<>")
let deviceTokenString = deviceToken.description.trimmingCharacters(in: characterSet).replacingOccurrences(of: " ", with: "");
print(deviceTokenString)
}


Related Topics



Leave a reply



Submit