iOS在应用程序内注册推送通知

Q1。 在我的应用程序的开始,我必须这样做吗? 或者我可以触发提示,允许/不允许在我的应用程序的任何时候?

Q2。 有没有办法找出用户是否点击是/否? (回电话?)

Q3。 如果用户已经点击了否,(在以前的会话中),我的提示实际上是否会触发? 或者我需要告诉用户去他们的电话设置,并启用它?

原因是我有一个应用程序里面有一个叫做“通知”的部分,他们可以启用/禁用接收某些事情的通知,所以我只想提示他们启用等等,当他们在本节不在应用程序的开始。

A1:不,不一定要在应用程序的开始。 您可以从代码中的任何位置调用registerForRemoteNotificationTypes。

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

你将需要处理下面的委托方法(在委托),这被称为成功/失败注册推送通知。

 // 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's database } - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { NSLog(@"Error in registration. Error: %@", err); } 

A2:是的,你可以。 有两种可能的情况。 如果您的应用程序没有运行,您将在didFinishLaunchingWithOptions中处理推送通知。 在这种情况下,如果用户在消息提示中select了“打开”,或者点击了横幅(取决于用户的设置),则应用程序将自动启动,并且可以处理在推送通知中传递的用户参数。

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

如果您的应用程序已经运行,则推送通知将被传递给application:didReceiveRemoteNotification: delegate方法,您可以在推送通知中简单地呈现带有消息的UIAlertView,并以标准方式处理alertView委托确定/取消button单击。

 - (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:如果用户拒绝接受来自你的应用程序的推送通知,那么它的didFailToRegisterForRemoteNotificationsWithError,因此你不会得到用户的devToken,它需要在你的服务器上发送推送通知给该用户。 如果用户最初接受,但稍后他更改了设置以禁用推送通知,则Apple服务器将不会将推送通知发送给该用户。 在这种情况下,用户的UDID将显示在反馈服务中,理想情况下,服务器应该从数据库中删除该用户的UDID,并停止向前进的用户发送推送通知。 如果您不断发送无效的推送通知,苹果服务器可能会放弃您的连接,您将无法发送任何推送通知。

有关实施的详细信息,请参阅Apple推送通知文档。

  • Q1没有必要在应用程序启动时进行推送注册,但有一些将它放在application: didFinishLaunchingWithOptions:以某种方式确保设备令牌成功存储在开发人员的服务器上。
  • Q2这里的“是/否”是什么? 如果您的意思是“如果接收推送通知”中的“是/否”,那么如果用户点击“是”,则application: didRegisterForRemoteNotificationsWithDeviceToken将被触发,否则将被触发。
  • Q3如果用户点击“否”拒绝接收推送通知,那么你以后可以提醒他在设置中打开它,或者有一个function,如UISwitch的东西,使用户能够触发[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)]; 再次。

你可以使用下面的教程swift( 访问链接 ),并使用以下简单的代码为Objective-C苹果推送通知。

  - (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); } 

在iOS 10中回答问题:2。

在iOS:10中,执行完成处理程序。 因此,您将在完成处理程序块中立即得到通知。

 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); }];