Delayed push notifications and checking if user en

2019-04-06 17:03发布

问题:

I develop an app, which builds itself around push notifications. The app requests notification permissions only when the user reaches certain point of the registration process. I have already managed to do the following:

  • the app maintains an NSUserDefaults variable, which indicates if it is required to register for push at launch or not (by default: not)
  • when the registration reaches that point, I flip the variable and call -registerForRemoteNotificationTypes: on iOS 7 and -registerUserNotificationSettings: on iOS 8

This works fine unless the user has already enabled push notifications and then disabled them later in the Settings. In this case I try to reregister Push at launch, which does not call either -application:didRegisterForRemoteNotificationsWithDeviceToken nor -application:didFailToRegisterForRemoteNotificationsWithError.

Additional information, that iOS 8's -isRegisteredForRemoteNotifications also returns YES. (I did not test but suppose that -enabledNotificationTypes works upto iOS 7.)

How can I detect this scenario and present the user a requester which asks him to reenable notifications in the Settings?

回答1:

[[UIApplication sharedApplication] 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 push notifications are actually available due to connectivity issues. The value returned by this method takes into account the user’s preferences for receiving push notifications.

Availability Available in iOS 8.0 and later.

Link to Apple Doc's

I would file a bug report at Apple.