1.JPush官网注册账号,新建应用。上传开发和生产推送证书.p12文件。注册完应用后生成AppKey和Master Secret。客户端只用到AppKey,Master Secret给后台用的。
2.拖到项目中,加依赖库。
3.设置background Modes
4.新建PushConfig.plist,加三个字段。如图。这个plist好像也可以不用。还不确定。。。因为代码中直接就能这三个字段的值了。不用从这读取。
5.复制代码。。。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[IQKeyboardManager sharedManager];
NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
//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];
}
//这个不写的话锁屏状态下就收不到消息
UIUserNotificationType type=UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound;
UIUserNotificationSettings *setting=[UIUserNotificationSettings settingsForTypes:type categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
//Required
<span style="color:#ff0000;">填写自己在jpush官网注册的时候得到的key</span>
NSString *appkey = @"7aaa3f0e9968adc27181ee20";
[JPUSHService setupWithOption:launchOptions appKey:appkey
channel:nil
apsForProduction:YES <span style="color:#ff0000;">发布的时候改为yes表示生产环境</span>
advertisingIdentifier:advertisingId];
return YES;
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
<span style="color:#ff0000;">根据用户名来推送到指定用户 这个方法在登录成功之后也要写一遍。</span>
if ([Cookie userId]) {
[JPUSHService setTags:[NSSet set] alias:[Cookie userId] fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
}];
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// IOS 7 Support Required
<span style="color:#ff0000;">收到推送消息</span>
NSLog(@"推送内容:%@",userInfo);
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}