首先要初始化sdk,官方文檔上有
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)? categories:nil];
[JPUSHService setupWithOption:launchOptions appKey:@"" channel:@"Publish channel" apsForProduction:NO];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//alias需要看自己的實際情況賦值
[JPUSHService setTags:nil alias:nil fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
}];
});
APPDelegate中加入方法
- (void)application:(UIApplication?*)application
didReceiveRemoteNotification:(NSDictionary?*)userInfo
fetchCompletionHandler:(void?(^)(UIBackgroundFetchResult))completionHandler {
[JPUSHService?handleRemoteNotification:userInfo];
NSLog(@"userInfo%@",userInfo);
completionHandler(UIBackgroundFetchResultNewData);
if?(application.applicationState?==?UIApplicationStateActive) {
//這里寫APP正在運行時,推送過來消息的處理
}?else?if?(application.applicationState?==?UIApplicationStateInactive?) {
//APP在后臺運行,推送過來消息的處理
[self?goToMssageViewControllerWith:userInfo];
}?else?if?(application.applicationState?==?UIApplicationStateBackground) {
//APP沒有運行,推送過來消息的處理
[self?goToMssageViewControllerWith:userInfo];
}
}
//跳轉方法
- (void)goToMssageViewControllerWith:(NSDictionary*)msgDic{
//將字段存入本地,在要跳轉的頁面用它來判斷
NSUserDefaults*pushJudge = [NSUserDefaults?standardUserDefaults];
[pushJudge?setObject:@"push"forKey:@"push"];
[pushJudge?synchronize];
if?([msgDic[@"type"]isEqualToString:@"Notification_cabinet"]) {
//這里寫要跳轉的controller
UserOrderListViewController?* VC = [[UserOrderListViewController?alloc]init];
UINavigationController?* Nav = [[UINavigationController?alloc]initWithRootViewController:VC];
[self.window.rootViewController?presentViewController:Navanimated:YES?completion:nil];
}?else?if?([msgDic[@"type"]isEqualToString:@"Message_notice"]) {
RecommendViewController?*vc = [[RecommendViewController?alloc]?init];
UINavigationController?* Nav = [[UINavigationController?alloc]initWithRootViewController:vc];
[self.window.rootViewControllerpresentViewController:Nav?animated:YES?completion:nil];
}
}
在要跳轉的頁面中的ViewDidLoad方法中加入
NSUserDefaults*pushJudge = [NSUserDefaults?standardUserDefaults];
if([[pushJudgeobjectForKey:@"push"]isEqualToString:@"push"]) {
[self.navigationController?setNavigationBarHidden:NOanimated:YES];
self.navigationItem.leftBarButtonItem?= [[UIBarButtonItem?alloc]?initWithImage:[UIImage?imageNamed:@""]style:UIBarButtonItemStylePlain target:selfaction:@selector(rebackToRootViewAction)];
}
具體情況根據自己的APP需要進行修改
轉載自? http://www.cnblogs.com/huihuihui/p/5865340.html