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

Bac*_*tnz 8 objective-c push-notification ios

Q1.在我的应用程序开始时我是否必须这样做?或者我可以在我的应用程序中的任何位置触发允许/不允许的提示吗?

Q2.有没有办法找出用户是否点击了是/否?(打回来?)

Q3.如果用户已经点击否,(在之前的会话中),我的提示是否会实际触发?或者我是否需要告诉用户转到他们的手机设置并在那里启用它?

原因是我有一个应用程序,里面有一个名为"通知"的部分,他们可以启用/禁用接收某些事情的通知,所以我只想提示他们启用等等,当他们在这部分而不是在应用程序的开始.

app*_*eak 21

A1:不,它不一定是应用程序的开头.您可以从代码中的任何位置调用registerForRemoteNotificationTypes.

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
Run Code Online (Sandbox Code Playgroud)

您将需要处理以下委托方法(在委托中),这些方法在成功/失败注册推送通知时被调用.

// 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);
}
Run Code Online (Sandbox Code Playgroud)

A2:是的,你可以.有两种可能的情况.如果您的应用程序未运行,您将在didFinishLaunchingWithOptions中处理推送通知.在这种情况下,如果用户在消息提醒中选择了"打开"或单击了横幅(取决于用户的设置),您的应用程序将自动启动,您可以处理推送通知中传递的用户参数.

 /* Push notification received when app is not running */
 NSDictionary *params = [[launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:@"appsInfo"];

    if (params) {
        // Use params and do your stuffs
    }
Run Code Online (Sandbox Code Playgroud)

如果您的应用程序已在运行,则推送通知将传递给application:didReceiveRemoteNotification:delegate方法,您可以在推送通知中显示带有消息的UIAlertView,并以标准方式处理alertView委托OK/Cancel按钮.

- (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
     }
}
Run Code Online (Sandbox Code Playgroud)

A3:如果用户拒绝接受来自您的应用程序的推送通知,那么它的didFailToRegisterForRemoteNotificationsWithError将导致您无法获得用户的devToken,该服务器需要在该服务器上向该用户发送推送通知.如果用户最初接受但稍后如果他更改了设置以禁用推送通知,那么Apple服务器将不会将推送通知发送给该用户.在这种情况下,用户的UDID将出现在反馈服务中,理想情况下,您的服务器应从数据库中删除该用户的UDID,并停止向该用户发送推送通知.如果您继续发送无效推送通知,Apple服务器可能会静默删除您的连接,您将无法发送任何推送通知.

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


Bab*_*nda 5

  • Q1没有必要在应用程序启动时放置推送寄存器,但有些确实将它放入application: didFinishLaunchingWithOptions:以某种方式确保设备令牌成功存储在开发人员的服务器上.
  • Q2这里的"是/否"是什么?如果你的意思是"如果收到推送通知"的"是/否",那么如果用户点击"是",application: didRegisterForRemoteNotificationsWithDeviceToken则会触发委托,否则不会.
  • Q3如果用户单击"否"拒绝接收推送通知,则您可以稍后提醒他在设置中打开它,或者具有UISwitch之类的功能,以使用户能够[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)]; 再次触发 .


小智 5

您可以使用以下swift教程(访问链接)并使用以下简单代码进行Apple推送通知的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);
    }
Run Code Online (Sandbox Code Playgroud)