美文网首页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-极光推送的使用

    1、首先先配置好推送证书,传到极光。极光推送->iOS证书设置指南极光推送->iOS SDK集成指南(XCode8...

  • 【知识总结】(2)远程推送

    推送SDK:极光推送 后台点击推送: iOS 10 以下收到推送点击触发 iOS 10 以上触发: 极光推送中使用...

  • ios极光推送

    第一次使用极光推送,在这里把极光推送的步骤说一下,省的以后再次用到极光推送的时候,给忘了,其实,极光推送不难...

  • 极光推送(二)——推送的使用

    前言 在极光推送(一)——配置中讲过了极光推送的配置,这节讲讲极光推送的使用参考文档极光官网 下面以我写的demo...

  • 极光推送进行远程推送

    借阅:极光推送进行远程推送 怎么使用极光推送进行远程推送 在极光官网注册极光推送创建一个应用在应用配置中导入两个证...

  • iOS-集成极光推送

    一:前言 第三方之推送极光推送 二.原理 1.请求APNS获得deviceToken2.得到APNS返回的devi...

  • iOS推送通知(极光推送)

    写在前面 要实现推送功能先要有苹果的推送证书:如何创建苹果推送证书要使用极光推送的功能,需要先集成极光推送SDK ...

  • React-Native 消息推送

    在这里我们可以选择大厂的推送,优先使用极光推送,下一篇将介绍如何使用阿里推送。 使用说明 PS: 真没想到极光大厂...

  • 在 android Notification使用PendingI

    项目的推送是采用极光推送,使用的是极光推送自定义消息,自己弹出通知栏,当有多天消息推送的时候PendingI...

  • [React Native] 极光推送,测试环境能收到,生产版本

    使用ReactNative集成极光,使用jpush-react-native 问题 极光推送,iOS测试环境能收到...

网友评论

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

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