最近因公司项目需求集成了最新的极光推送,个人感觉比苹果官方的推送快、及时,集成起来也比较方便所以推荐使用。废话不多说进入正题。
1.多看下官方文档,上面写的很详细,链接 http://docs.jpush.io/client/ios_tutorials/
2.�至于如何配置证书这些我这里就不做累赘了,不过一定要仔细哦!很多童靴这步搞错。大家可以看看文档!
3.它集成起来本身所需的代码量不多,需要导入的库很少,文档中有介绍可以把demo下载下来看看,
4.具体的代码实现了,得导入 #import"APService.h"头文件,在didFinishLaunchingWithOptions里面注册极光推送,重点
NSNotificationCenter*defaultCenter = [NSNotificationCenterdefaultCenter];
[defaultCenteraddObserver:self
selector:@selector(networkDidSetup:)
name:kJPFNetworkDidSetupNotification
object:nil];
[defaultCenteraddObserver:self
selector:@selector(networkDidClose:)
name:kJPFNetworkDidCloseNotification
object:nil];
[defaultCenteraddObserver:self
selector:@selector(networkDidRegister:)
name:kJPFNetworkDidRegisterNotification
object:nil];
[defaultCenteraddObserver:self
selector:@selector(networkDidLogin:)
name:kJPFNetworkDidLoginNotification
object:nil];
[defaultCenteraddObserver:self
selector:@selector(networkDidReceiveMessage:)
name:kJPFNetworkDidReceiveMessageNotification
object:nil];
[defaultCenteraddObserver:self
selector:@selector(serviceError:)
name:kJPFServiceErrorNotification
object:nil];
这里也给大家看看苹果本身的推送注册,看下区别
//if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
//{
//[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
//settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
//categories:nil]];
//[[UIApplication sharedApplication] registerForRemoteNotifications];
//
//}
//else
//{
////这里还是原来的代码
//[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
//}
剩下就是复制粘贴了
-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString*token = [NSStringstringWithFormat:@"%@", deviceToken];
NSLog(@"My token is:%@", token);
token = [JsonToolsformatToken:token];
NSUserDefaults*userDefaults = [NSUserDefaultsstandardUserDefaults];
[userDefaultssetObject:tokenforKey:kDeviceToken];
[APServiceregisterDeviceToken:deviceToken];
}
-(void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSString*error_str = [NSStringstringWithFormat:@"%@", error];
NSLog(@"Failed to get token, error:%@", error_str);
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
- (void)application:(UIApplication*)application
didRegisterUserNotificationSettings:
(UIUserNotificationSettings*)notificationSettings {
}
// Called when your app has been activated by the user selecting an action from
// a local notification.
// A nil action identifier indicates the default action.
// You should call the completion handler as soon as you've finished handling
// the action.
- (void)application:(UIApplication*)application
handleActionWithIdentifier:(NSString*)identifier
forLocalNotification:(UILocalNotification*)notification
completionHandler:(void(^)())completionHandler {
}
// Called when your app has been activated by the user selecting an action from
// a remote notification.
// A nil action identifier indicates the default action.
// You should call the completion handler as soon as you've finished handling
// the action.
- (void)application:(UIApplication*)application
handleActionWithIdentifier:(NSString*)identifier
forRemoteNotification:(NSDictionary*)userInfo
completionHandler:(void(^)())completionHandler {
}
#endif
-(void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
[APServicehandleRemoteNotification:userInfo];
NSLog(@"推送-------%@",userInfo);
}
- (void)application:(UIApplication*)application
didReceiveRemoteNotification:(NSDictionary*)userInfo
fetchCompletionHandler:
(void(^)(UIBackgroundFetchResult))completionHandler {
[APServicehandleRemoteNotification:userInfo];
NSLog(@"收到通知:%@",userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication*)application
didReceiveLocalNotification:(UILocalNotification*)notification {
[APServiceshowLocalNotificationAtFront:notificationidentifierKey:nil];
}
这是它们本身的回调大家可以看哪里出问题:
- (void)networkDidSetup:(NSNotification*)notification {
NSLog(@"已连接");
}
- (void)networkDidClose:(NSNotification*)notification {
NSLog(@"未连接");
}
- (void)networkDidRegister:(NSNotification*)notification {
NSLog(@"%@", [notificationuserInfo]);
NSLog(@"已注册");
}
- (void)networkDidLogin:(NSNotification*)notification {
NSLog(@"已登录");
if([APServiceregistrationID]) {
NSLog(@"get RegistrationID------%@", [APServiceregistrationID]);
}
}
最后这点大家要注意,设置alias;不然收不到推送的
[APServicesetTags:nil
alias:str
callbackSelector:nil
target:self];
今天就先这样!