美文网首页iOS 开发 iOS Developer ios零碎记录
iOS 非常简单的 极光推送 -- LZ

iOS 非常简单的 极光推送 -- LZ

作者: flutter养成中 | 来源:发表于2016-04-22 14:52 被阅读335次

    极光: https://www.jpush.cn

    1. 先申请 获取appKey;

    2.程序入口中 添加代码(就是这么简单 , 你只需要复制粘贴):

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //极光推送
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
    
    NSMutableSet *categories = [NSMutableSet set];
    
    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
    
    category.identifier = @"identifier";
    
    UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
    
    action.identifier = @"test2";
    
    action.title = @"test";
    
    action.activationMode = UIUserNotificationActivationModeBackground;
    
    action.authenticationRequired = YES;
    
    //YES显示为红色,NO显示为蓝色
    
    action.destructive = NO;
    
    NSArray *actions = @[ action ];
    
    [category setActions:actions forContext:UIUserNotificationActionContextMinimal];
    
    [categories addObject:category];
    
    [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)                      categories:categories];
    
    }else{
    
    [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)                      categories:nil];
    
    }
    
    //启动SDK
    
    [JPUSHService setupWithOption:launchOptions appKey:JPUSH_APPKEY channel:JPUSH_CHANNEL apsForProduction:isProduction];
    
    //如果 App 状态为未运行,此函数将被调用,如果launchOptions包含UIApplicationLaunchOptionsRemoteNotificationKey表示用户点击apn 通知导致app被启动运行;如果不含有对应键值则表示 App 不是因点击apn而被启动,可能为直接点击icon被启动或其他。
    
    //    NSDictionary *remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    
    [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
    
    
    
    [JPUSHService registrationID]   //注册    这里返回的是字符串   可以接收,有可能要上传服务器,具体看需求
    
    }
    
    

    3.继续添加:

    #pragma mark 注册消息推送失败
    
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
    
    {
    
    NSLog(@"消息推送失败");
    
    }
    
    
    #pragma mark 处理收到的消息推送
    
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    
    {
    
    //获取 APNS 标准信息内容
    
    NSDictionary *aps = [userInfo valueForKey:@"aps"];
    
    NSString *content = [aps valueForKey:@"alert"];//推送显示的内容
    
    int badge = [[aps valueForKey:@"badge"] intValue];//badge数量
    
    NSString *sound = [aps valueForKey:@"sound"];//播放的声音
    
    //取得Extras字段内容(额外的附加信息)
    
    NSString *customizeField1 = [userInfo valueForKey:@"customizeExtras"];//服务端中Extras字段,key是自己定义的
    
    NSLog(@"content =[%@], badge=[%ld], sound=[%@], customize field  =[%@]",content, (long)badge,sound,customizeField1);
    
    UILocalNotification *local = [JPUSHService setLocalNotification:[NSDate dateWithTimeIntervalSinceNow:1] alertBody:content badge:badge alertAction:@"OK" identifierKey:@"key" userInfo:userInfo soundName:sound region:nil regionTriggersOnce:YES category:nil];
    
    [JPUSHService showLocalNotificationAtFront:local identifierKey:@"key"];
    
    if (application.applicationState == UIApplicationStateActive) {
    
    // 转换成一个本地通知,显示到通知栏,你也可以直接显示出一个alertView,只是那样稍显aggressive:)
    
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    
    localNotification.userInfo = userInfo;
    
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    
    localNotification.alertBody = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
    
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
    
    //  收到本地通知 badgeNumber ++;
    
    NSInteger badgeNumber = application.applicationIconBadgeNumber;
    
    badgeNumber ++;
    
    localNotification.applicationIconBadgeNumber = badgeNumber;
    
    // ios8后,需要添加这个注册,才能得到授权
    
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    
    UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
    
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
    
    categories:nil];
    
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    
    // 通知重复提示的单位,可以是天、周、月
    
    localNotification.repeatInterval = NSCalendarUnitDay;
    
    }
    
    //        localNotification.
    
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    
    }
    
    //Required
    
    [JPUSHService handleRemoteNotification:userInfo];
    
    completionHandler(UIBackgroundFetchResultNewData);
    
    }
    
    #pragma mark 获取自定义消息推送内容
    
    - (void)networkDidReceiveMessage:(NSNotification *)notification
    
    {
    
    NSDictionary *userInfo = [notification userInfo];
    
    //    NSString *content = [userInfo valueForKey:@"content"];
    
    //    NSDictionary *extras = [userInfo valueForKey:@"extras"];
    
    //    NSString *customizeField1 = [extras valueForKey:@"customizeField1"];
    
    NSLog(@"dadasdasda");
    
    NSLog(@"%@",userInfo);
    
    }
    

    进行处理,比如对badgeNumber的增减,也就是app外面的那个红色数字提示,以及本地通知的处理:

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification   //  后台点击通知或者前台接受到通知时  会走
    
    {
    
    //  点击本地通知 badgeNumber --;
    
    NSInteger badgeNumber = application.applicationIconBadgeNumber;
    
    badgeNumber --;
    
    application.applicationIconBadgeNumber = badgeNumber;
    
    NSArray *localNotifications = application.scheduledLocalNotifications;
    
    if (localNotifications.count >= 64) {
    
    //  消息上限  删除所有的本地通知
    
    [application cancelAllLocalNotifications];
    
    application.applicationIconBadgeNumber = 0;
    
    }
    
    //  接收到了就删除 本地通知
    
    [application cancelLocalNotification:notification];
    
    //  重新设置 APP 的 BadgeNumber
    
    NSArray *NowlocalNotifications = application.scheduledLocalNotifications;
    
    application.applicationIconBadgeNumber = NowlocalNotifications.count;
    
    }
    

    相关文章

      网友评论

        本文标题:iOS 非常简单的 极光推送 -- LZ

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