iOS推送

作者: 不多满 | 来源:发表于2016-01-18 19:05 被阅读380次
    一、推送分为两种
    一种是本地消息(Local Notificatio),一种是远程消息(Push Notification).  
    
    二、推送的效果
    UIRemoteNotificationTypeBadge 在应用图标上面显示角标
    UIRemoteNotificationTypeSound 声音提醒
    UIRemoteNotificationTypeAlert 通过alert或者是banner展示内容
    

    三、本地推送

    代码示例:

    1、注册推送
    //iOS8以后,使用推送需要用户授权
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else  {
        [[UIApplication sharedApplication]registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
    }
    
    2、重写代理方法,查看“注册推送”的状态
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        NSString* token = [NSString stringWithFormat:@"%@", deviceToken];
        NSLog(@"Register ok! %@", token);
     }
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
        NSLog(@"Register failed! %@", error);
    }
    
    3、添加通知
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UILocalNotification *localNoti = [[UILocalNotification alloc] init];
        localNoti.alertBody = @"这是一个推送的示例!!!";
        localNoti.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; //设置5秒后发起通知
        localNoti.soundName = @"default";
        localNoti.applicationIconBadgeNumber = 1; //设置角标的数值
        [[UIApplication sharedApplication] scheduleLocalNotification:localNoti];
    }
    

    至此,一个简单的本地推送示例就完成了。

    四、远程推送

    1、原理
    1:app服务器把要发送的消息、目的设备的唯一标识打包,发给APNs。
    2:APNs根据设备的唯一标识,把消息发送到设备。
    3:app按照推送类型展示:是否需要声音,是否有角标,以及banner。
    ⚠️ 远程推送需要有推送证书。
    
    2、实现
    (1)、注册推送
    //iOS8以后,使用推送需要用户授权
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else  {
        [[UIApplication sharedApplication]registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
    }
    
    (2)、重写代理方法,查看“注册推送”的状态,并上传deviceToken至app服务器
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        NSString* token = [NSString stringWithFormat:@"%@", deviceToken];
        NSLog(@"Register ok! %@", token);
        //上传deviceToken到app的服务器
        [KKUtils setTokenID:deviceToken];
     }
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
        NSLog(@"Register failed! %@", error);
    }
    
    3、添加通知
    这个具体在app的服务器中实现。

    相关文章

      网友评论

      • 189fe6811131:请问博主第三方推送和这个推送有什么区别?

      本文标题:iOS推送

      本文链接:https://www.haomeiwen.com/subject/srukkttx.html