zoukankan      html  css  js  c++  java
  • iOS开发——极光推送

       1.到极光官网 https://www.jpush.cn/ 下载极光推送SDK。

         具体如何集成最好参考官网的文档,以及一些失败的原因。文档非常详细,我也是参考集成的。

       2.到极光推送官网注册自己的应用信息,关键是参考官网文档把推送证书p12文件上传好。http://docs.jpush.io/client/ios_tutorials/#ios_1

       

    3.将下载好的SDK解压的文件拖到项目中,并添加一下框架

    • CFNetwork.framework
    • CoreFoundation.framework
    • CoreTelephony.framework
    • SystemConfiguration.framework
    • CoreGraphics.framework
    • Foundation.framework
    • UIKit.framework
    • Security.framework
    • Xcode7需要的是libz.tbd;Xcode7以下版本是libz.dylib

    4.可以参考我的其他博客,设置XCode7支持Http传输方法。

    5.还需入下图配置下项目

     

    6.在AppDelegate中,导入#import "JPUSHService.h"  

      建议在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中单独设置一个方法调用,防止和其他的设置混乱
        
        // 极光推送设置
        [self JPush:launchOptions];

    7.以下是AppDelegate.m中主要需要实现的代码。包括设置AppKey

    - (void)JPush:(NSDictionary *)launchOptions{
        
        // 程序未运行时,接收消息启动app
        NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
        NSLog(@"未运行收到的消息%@",remoteNotification);
    
    //#ifdef __IPHONE_8_0
        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];
        }
        
    //#endif
        // Required
        [JPUSHService setupWithOption:launchOptions appKey:@"ef7**********3d87" channel:@"Publish channel" apsForProduction:YES]; //如需兼容旧版本的方式,请继续使用[JPUSHService setupWithOption:launchOptions]初始化方法和添加pushConfig.plist文件声明AppKey等配置内容。
        
        
        // 开启调试模式debug
        [JPUSHService setDebugMode];
        
        
        //  接受自定义消息
        NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
        [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
        NSNotificationCenter *defaultCenter1 = [NSNotificationCenter defaultCenter];
        [defaultCenter addObserver:self selector:@selector(JPushID:) name:kJPFNetworkDidLoginNotification object:nil];
    
    
    }
    
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        
        // Required
        [JPUSHService registerDeviceToken:deviceToken];
        NSLog(@"注册的token%@",deviceToken);
    }
    
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
        
        //Optional
        NSLog(@"注册远程通知失败did Fail To Register For Remote Notifications With Error: %@", error);
    }
    
    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    
        NSLog(@"++++本地收到的消息====%@",notification);
    
    }
    
    // 应程序在前台或后台运行时接收消息
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
        
         NSLog(@"获取到的APN推送内容: %@",userInfo);
        
        
        // IOS 7 Support Required
        [JPUSHService handleRemoteNotification:userInfo];
        completionHandler(UIBackgroundFetchResultNewData);
        
       
    }
    
    
    // 获取极光返回的用户ID
    - (void)JPushID:(NSNotification *)notification{
    
        NSLog(@"获取到的registrationID===:%@",[JPUSHService registrationID]);
    
    }
    
    // 通知方法回调:接受自定义消息
    - (void)networkDidReceiveMessage:(NSNotification *)notification {
        NSDictionary * userInfo = [notification userInfo];
        NSString *content = [userInfo valueForKey:@"content"];
        NSDictionary *extras = [userInfo valueForKey:@"extras"];
        NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //自定义参数,key是自己定义的
       // NSString *JPushID       = [];
     
        NSLog(@"接收自定义消息:%@",content);
        
       
        
        
    }

    8.这样可以单独在极光官网测试推送,最好单独获取到的registrationID单个推送测试。

      我开始可以内网推送,但走苹果APNS失败,最后还是推送证书的问题。重新做的并上传到极光推送逛网。

  • 相关阅读:
    CF1051F The Shortest Statement 题解
    CF819B Mister B and PR Shifts 题解
    HDU3686 Traffic Real Time Query System 题解
    HDU 5969 最大的位或 题解
    P3295 萌萌哒 题解
    BZOJ1854 连续攻击游戏 题解
    使用Python编写的对拍程序
    CF796C Bank Hacking 题解
    BZOJ2200 道路与航线 题解
    USACO07NOV Cow Relays G 题解
  • 原文地址:https://www.cnblogs.com/LiuChengLi/p/4971543.html
Copyright © 2011-2022 走看看