iOS 远程推送

在AppDelegate.m里面的 application:didFinishLaunchingWithOptions方法内注册推送以获取deviceToken 

    //注册推送
    UIUserNotificationType usertype = (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert);
    UIRemoteNotificationType remotetype = (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert);
    
    if (iOS8) {
        UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:usertype categories:nil];
        [application registerUserNotificationSettings:uns];
        [application registerForRemoteNotifications];
    }else{
        [application registerForRemoteNotificationTypes:remotetype];
    }



在 AppDelegate.m 里面加入这几个方法

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"注册推送成功,需要把deviceToken提交给服务器");
    NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken:%@",deviceToken);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"注册推送失败");
    NSLog(@"didFailToRegisterForRemoteNotificationsWithError:%@",error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSLog(@"收到一条服务器推送消息");
    NSLog(@"didReceiveRemoteNotification:%@",userInfo);
}



你可能感兴趣的:(iOS 远程推送)