美文网首页iOS开发
iOS-极光推送的使用

iOS-极光推送的使用

作者: BestVast | 来源:发表于2016-09-14 14:17 被阅读535次
    You've implemented -[<UIApplicationDelegate>
    application:didReceiveRemoteNotification:fetchCompletionHandler:], 
    but you still need to add "remote-notification" to the list of
    your supported UIBackgroundModes in your Info.plist.
    
    • 4、导入必要的框架


      push3.png
    • 5、AppDelegate.m代码配置
    //极光推送
    #import "JPUSHService.h"
    
    static NSString *appKey = @"注册极光推送App的key值";//(与plist文件里面的相同)
    static NSString *channel = @"Publish channel";
    static BOOL isProduction = NO;
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        [self setJpushWithOptions:launchOptions];
        
        return YES;
    }
    #pragma mark - "设置极光推送问题"
    - (void)setJpushWithOptions:(NSDictionary *)launchOptions
    {
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            //可以添加自定义categories
            [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                              UIUserNotificationTypeSound |
                                                              UIUserNotificationTypeAlert)
                                                  categories:nil];
        } else {
            //categories 必须为nil
            [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                              UIRemoteNotificationTypeSound |
                                                              UIRemoteNotificationTypeAlert)
                                                  categories:nil];
        }
        
        [JPUSHService setupWithOption:launchOptions appKey:appKey
                              channel:channel apsForProduction:isProduction];
    }
    
    - (void)application:(UIApplication *)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        
        NSLog(@"\n-->%@<--\n", [NSString stringWithFormat:@"Device Token: %@", deviceToken]);
        [JPUSHService registerDeviceToken:deviceToken];
    }
    
    #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
    - (void)application:(UIApplication *)application
    didRegisterUserNotificationSettings:
    (UIUserNotificationSettings *)notificationSettings
    {
        
    }
    - (void)application:(UIApplication *)application
    handleActionWithIdentifier:(NSString *)identifier
    forLocalNotification:(UILocalNotification *)notification
      completionHandler:(void (^)())completionHandler
    {
        
    }
    - (void)application:(UIApplication *)application
    handleActionWithIdentifier:(NSString *)identifier
    forRemoteNotification:(NSDictionary *)userInfo
      completionHandler:(void (^)())completionHandler
    {
        
    }
    #endif
    
    - (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo
    {
        [JPUSHService handleRemoteNotification:userInfo];
        NSLog(@"通知内容%@", userInfo);
    }
    
    - (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:
    (void (^)(UIBackgroundFetchResult))completionHandler
    {
        [JPUSHService handleRemoteNotification:userInfo];
        NSLog(@"收到通知:%@", [self logDic:userInfo]);
        NSLog(@"%@",userInfo);
        completionHandler(UIBackgroundFetchResultNewData);
        // 取得 APNs 标准信息内容,如果没需要可以不取
        NSDictionary *aps = [userInfo valueForKey:@"aps"];
        NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容
        //设置badge的角标
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[aps valueForKey:@"badge"] integerValue]];
        //    NSInteger badge = [[aps valueForKey:@"badge"] integerValue];
        //    NSString *sound = [aps valueForKey:@"sound"]; //播放的声音
        if (application.applicationState == UIApplicationStateActive) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"state is active" message:content delegate:nil cancelButtonTitle:@"YES" otherButtonTitles:nil];
            [alert show];
            
            [application setApplicationIconBadgeNumber:0];//通过此方法清除角标
        }
        else {
            [[NSUserDefaults standardUserDefaults] setObject:userInfo forKey:@"PushUserInfo"];
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"state is not active" message:content delegate:nil cancelButtonTitle:@"YES" otherButtonTitles:nil];
            [alert show];
            
            [application setApplicationIconBadgeNumber:0];//通过此方法清除角标
            //        NotificationViewController *notification = [[NotificationViewController alloc] init];
            //        UINavigationController *naviNoti = [[UINavigationController alloc] initWithRootViewController:notification];
            //        [self.window.rootViewController presentViewController:naviNoti animated:YES completion:nil];
            
        }
    }
    
    // log NSSet with UTF8   if not ,log will be \Uxxx
    - (NSString *)logDic:(NSDictionary *)dic
    {
        if (![dic count]) {
            return nil;
        }
        NSString *tempStr1 =
        [[dic description] stringByReplacingOccurrencesOfString:@"\\u"
                                                     withString:@"\\U"];
        NSString *tempStr2 =
        [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
        NSString *tempStr3 =
        [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
        NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
        NSString *str =
        [NSPropertyListSerialization propertyListFromData:tempData
                                         mutabilityOption:NSPropertyListImmutable
                                                   format:NULL
                                         errorDescription:NULL];
        //  NSString *str = [NSPropertyListSerialization propertyListWithData:tempData options:NSPropertyListImmutable format:NULL error:nil];
        return str;
    }
    
    - (void)application:(UIApplication *)application
    didReceiveLocalNotification:(UILocalNotification *)notification
    {
        [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
    }
    - (void)application:(UIApplication *)application
    didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
    {
        NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
    }
    
    • 6、设置设备别名(单独给用户推送)-> 这个需要极光推送在AppDelegate.m里面注册完成之后再调用此方法
    [JPUSHService setAlias:@"haha" callbackSelector:nil object:self];
    

    相关文章

      网友评论

        本文标题:iOS-极光推送的使用

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