Controlling when notification permissions dialog is shown (iOS)

On iOS the registerForRemoteNotificationTypes method used when initialising XtremePush in your App Delegate will cause the push notifications system permissions dialog to appear:  

 Objective-C:

[XPush registerForRemoteNotificationTypes:types];

Swift:

XPush.registerForRemoteNotificationTypes(notifications!)


System permissions dialog

You may want to delay the request for notification permissions until the user has logged in or until they have reached an appropriate section in the app where the permission is clearly required for example turning on an alerting function.  It is also good practice to display a value exchange screen prior to asking for notification permissions explaining why you want to use notifications. This screen can contain App level Allow and Don't Allow buttons, that can set a local flag in the app if the user says no. If the user says no you don't have to show the OS dialog and that means if you want to ask them sometime in the future, you haven’t burned your chance with the OS permissions dialog. 

If you want to control when the notification permissions dialog is shown then remove the call to the registerForRemoteNotificationTypes method from your App Delegate. You can call it later in conjunction with some additional logic related to your custom push permissions prompt workflow. This workflow can contain a value exchange and logic to ask again later if the user says opts out at the custom dialog initially.

 

 

   Simple Value exchange screen 

 

Objective-C:

For example you could have a function linked to an “Allow” button like this:


- (IBAction)pushAllowAction:(id)sender {
if (self.didTapOnPushButtonHandler) {

[[NSUserDefaults standardUserDefaults] setBool:YES forKey: push];
[[NSUserDefaults standardUserDefaults] synchronize];

NSInteger types;

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.f) {
types = UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound;
} else {
types = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge;
}
[XPush registerForRemoteNotificationTypes:types];

}
}

And a function linked to the “Set up later" button like this:


- (IBAction)declineAction:(id)sender {
if (self.didTapOnDeclineButtonHandler) {

[[NSUserDefaults standardUserDefaults] setObject:[self addMonthtoDate] forKey: push];
[[NSUserDefaults standardUserDefaults] synchronize];

}
}

Swift:

Similarly you could have a function linked to the “Allow” button like this:


   func approvePushButton(){

       defaults.setObject("approved", forKey: "push")
       approvalComplete()
       var notifications: NSInteger?

     
       if #available(iOS 8.0, *){

           let temp: UIUserNotificationType = [.Alert, .Badge, .Sound]
           notifications = NSInteger(temp.rawValue)

       } else {

           let temp: UIRemoteNotificationType = [.Sound, .Alert, .Badge]
           notifications = NSInteger(temp.rawValue)

       }

       XPush.registerForRemoteNotificationTypes(notifications!)

   }

And a function linked to the “Set up later" button like this:

 
   func declinePushButton(){

       defaults.setObject(addMonthToDate(), forKey: "push")
       approvalComplete()

   }
Have more questions? Submit a request

0 Comments

Article is closed for comments.