IOS Registration for Push Notifications in the App

Q1. Should I do this at the beginning of my application? Or can I raise an allow / not allow prompt at any time in my application?

Q2. Is there any way to find out if the user clicked yes / no? (Callback?)

Q3. If the user has not already clicked the No button (in the previous session), will my prompt begin? Or do I need to tell the user to go to the phone settings and turn it on there?

I have an application in which there is a section inside it called "Notifications", through which they can enable / disable the receipt of notifications about some things, so I only want to invite them to turn on, etc. when they are in this section a not at the beginning of the application.

+7
source share
4 answers

A1: No, this does not have to be at the beginning of the application. You can call registerForRemoteNotificationTypes from anywhere in the code.

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; 

You will need to handle the following delegate methods (in the delegate) that are called upon successful registration / failure for push notification.

 // Delegation methods - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken { const void *devTokenBytes = [devToken bytes]; self.registered = YES; [self sendProviderDeviceToken:devTokenBytes]; // this will send token to your server database } - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { NSLog(@"Error in registration. Error: %@", err); } 

A2: Yes you can. Two scenarios are possible. If your application is not running, you will process a push notification in the didFinishLaunchingWithOptions file. In this case, if the user selects “open” in the message or clicks on “Banners” (depending on user settings), your application will automatically start and you will be able to process user parameters sent in a push notification.

  /* Push notification received when app is not running */ NSDictionary *params = [[launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:@"appsInfo"]; if (params) { // Use params and do your stuffs } 

If your application is already running, the push notification will be delivered to the application:didReceiveRemoteNotification: delegate method, where you can simply present the UIAlertView with the message in the push notification and process delegate delegates alertView OK / Cancel by clicking in the standard way.

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSDictionary *apsInfo = [userInfo objectForKey:@"apsinfo"]; // This appsInfo set by your server while sending push NSString *alert = [apsInfo objectForKey:@"alert"]; UIApplicationState state = [application applicationState]; if (state == UIApplicationStateActive) { application.applicationIconBadgeNumber = 0; AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Push Notification" message:alert delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES"]; [alertview show]; [alertview release]; } else { [self setTabs:contentsInfo]; } } - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex { if (buttonIndex != [alertView cancelButtonIndex]) { // User pressed YES, do your stuffs } } 

A3: If the user refused to accept the push notification from your application, then his didFailToRegisterForRemoteNotificationsWithError and, therefore, you will not receive the user devToken, which must be on your server to send a push notification to this user. If the user first accepts, but later, if he changes the settings to turn off your push notification, the Apple server will not send your push notification to this user. In this case, the user UDID will appear in the feedback service, and ideally, your server should remove this user UDID from the database and stop sending push notifications to these users. If you continue to send invalid push notifications, the Apple server may disconnect your connection and you will not be able to send any push notifications.

For details on the implementation, see Apple Push Notification .

+21
source
  • Q1 You do not need to set the push register when the application starts, but some do put it in application: didFinishLaunchingWithOptions: to somehow make sure that the device token is successfully stored on the developer's server.
  • Q2 What does yes / no mean here? If you mean yes / no to if you receive a push notification, then if the user clicked yes, the application: didRegisterForRemoteNotificationsWithDeviceToken will be launched, otherwise no.
  • Q3 If a user clicks “no” to reject receiving a push notification, then either you can remind him to enable it in the settings, or a function like UISwitch to allow users to run [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)]; again.
+5
source

You can use the following tutorial for a quick visit ( Visit Link ) and use the following simple code for objective-c for Apple Push Notification.

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) { // iOS 8 Notifications [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound |UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [application registerForRemoteNotifications]; } return YES; } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"Did Register for Remote Notifications with Device Token (%@)", deviceToken); } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"Did Fail to Register for Remote Notifications"); NSLog(@"%@, %@", error, error.localizedDescription); } -(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { NSLog(@"%@",userInfo); } 
+5
source

Answer the question: 2 in iOS 10.

In iOS: 10, a completion handler is implemented. Therefore, you will be notified immediately in the completion handler block.

 if([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.f){ UNUserNotificationCenter* notificationCenter = [UNUserNotificationCenter currentNotificationCenter]; [notificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge completionHandler:^(BOOL granted, NSError * _Nullable error) { NSLog(@"grant Access:%d",granted); }]; 
0
source

All Articles