user2732722
user2732722

Reputation: 655

Can you allow push notifications in the app programmatically?

I use push notifications in my app and as its expected the user sees a dialog box asking to allow the push notifications when the app starts. The logic of my app depends on receiving push notifications and if the user didn't allow the push notification by mistake or not knowing how the application works, the app won't work correctly. Is there a way to allow push notifications in the app programmatically even if the user doesn't allow it? Thanks!

Upvotes: 0

Views: 1231

Answers (2)

纪忠懿
纪忠懿

Reputation: 124

Unfortunately you can't do like this.Before iOS 8 You can detect the APNS state use

UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

or after iOS 8:

UIRemoteNotificationType enableType = [[UIApplication sharedApplication] currentUserNotificationSettings];

or Swift:

let remoteState = UIApplication.sharedApplication().currentUserNotificationSettings()

You can prompt users if they close APNS or launch the App .Such as: You have closed the APNS and you will lose some important message.Please go to Settings -> Notifications -> App to enable APNS

or you can jump to your app page after iOS8 :

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
   BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL);
        if (canOpenSettings) {
            NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [[UIApplication sharedApplication] openURL:url];
            }}

Upvotes: 1

Tom Harrington
Tom Harrington

Reputation: 70946

No. If the user says you can't sent them push notifications, you can't send them push notifications. Apps don't get to ignore the user's choice there.

However if you're sending silent push notifications-- those that get delivered to your app but not displayed to the user-- you don't need to get permission. If that suits your needs, great, but you don't get to have user-visible pushes without permission.

Upvotes: 3

Related Questions