I’m registering my iPhone application for Remote Notification.
Here my scenario:
- User opens my app and register for remote notification
- User turns notification off for my app
- User open my app
- User turns notification on for my app
My questions are:
- Is it possible to check if the user has disabled remote notification for my app inside my app’s code?
- If I request a new token without user turn off notification for my app, the retrieved token is different or is always the same?
- Should I request a new token every time I start my app?
So you want to know about UIRemoteNotifications? 🙂 Cool, because they’re not really as complex as people often make them out to be. Though, I am going to address your questions in reverse order. It flows better than way.
Your questions:
Sort of. With UIRemoteNotifications, you never really request a token, so much as request permission and receive a token. What you should do is implement
application:didRegisterForRemoteNotificationsWithDeviceToken:in your app delegate. This method (along with its error-catching siblingapplication:didFailToRegisterForRemoteNotificationsWithError:) is the callback forregisterForRemoteNotificationTypes:. It’s best practice to callregisterForRemoteNotificationTypes:duringapplication:didFinishLaunchingWithOptions:. (Don’t worry about all of the method names flying around. I’ll explain codewise shortly).Maybe. The device token is subject to change for security reasons, but in general you shouldn’t need to be too concerned with it changing.
Why, yes it is.
UIApplicationDelegatehas a method calledenabledRemoteNotificationTypes, which gets all of the remote notification types requested by you and enabled by your user. More on that shortly.Putting it all together:
At the end of the day, you should end up with something like this: