您的位置:首页 > 移动开发 > IOS开发

iOS_极光推送

2016-06-09 00:25 453 查看
1、在

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions

调用自定义的方法,使用极光推送

[self
registerPushNotificationWithlaunchOptions:launchOptions];

- (void)registerPushNotificationWithlaunchOptions:(NSDictionary *)launchOptions
{
// 向apple服务器注册通知服务
[self registerForRemoteNotification];
// 初始化JPUSH参数
[self setupJPUSHWithOption:launchOptions];
}


2.1、向apple服务器注册通知服务

/**
*  @programmer shuaixiao

*  @brief  向apple服务器注册通知服务

*  @param  <#无#>

*  @return <#无#>

*/
- (void)registerForRemoteNotification
{
//Required
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];
}
}


2.2、初始化JPUSH参数

/**
*  @programmer shuaixiao

*  @brief  初始化JPUSH参数

*  @param  <#无#>

*  @return <#无#>

*/
- (void)setupJPUSHWithOption:(NSDictionary *)launchOptions
{
//Required
// 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
NSString *configPath = [[NSBundle mainBundle]pathForResource:@"PushConfig.plist" ofType:nil];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:configPath];
NSString *appKey = [dict objectForKey:@"APP_KEY"];
NSString *channel = [dict objectForKey:@"CHANNEL"];
NSString *isProduction = [dict objectForKey:@"APS_FOR_PRODUCTION"];
[JPUSHService setupWithOption:launchOptions appKey:appKey
channel:channel
apsForProduction:isProduction
advertisingIdentifier:nil];
}


3、

将deviceToken上传到第3方服务器 和 自己的服务器
/**
* @programmer shuaixiao

* @brief 将deviceToken上传到服务器

* @param <#无#>

* @return <#无#>

*/
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
/// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
//----- register result -----
//uid: 4949826728
//registrationID:171976fa8a80077f0b3
}

4、处理接收到的消息
/**
* @programmer shuaixiao

* @brief 前台、后台、退出时均可收到消息

* @param <#无#>

* @return <#无#>

*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
kLogAndShowMessage(@"来消息了")
[self handleWithNotificationDict:userInfo];

// IOS 7 Support Required
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}

5、跳转代码
- (void)handleWithNotificationDict:(NSDictionary *)userInfo
{
// 写跳转代码即可
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
UILabel *label = [[UILabel alloc]initWithFrame:keyWindow.frame];
label.text = [userInfo description];
label.numberOfLines = 0;
label.alpha = 0.8;
label.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
[keyWindow addSubview:label];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS 极光推送