AppDelegate中:
#import <JPUSHService.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//設(shè)置push
[self setupPush:application didFinishLaunchingWithOptions:launchOptions];
return YES;
}
- (void)setupPush:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
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];
}
// * @param isProduction 是否生產(chǎn)環(huán)境. 如果為開發(fā)狀態(tài),設(shè)置為 NO; 如果為生產(chǎn)狀態(tài),應(yīng)改為 YES.
[JPUSHService setupWithOption:launchOptions appKey:JPushKey
channel:@""
apsForProduction:YES
advertisingIdentifier:nil];
[application setApplicationIconBadgeNumber:0];// 去掉提示紅點
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Required
[JPUSHService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Required
[JPUSHService handleRemoteNotification:userInfo];
[self handlerPush:application addParam:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// IOS 7 Support Required
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
//自定義處理push內(nèi)容
[self handlerPush:application addParam:userInfo];
}
-(void)handlerPush:(UIApplication *)application addParam:(NSDictionary*)userInfo{
[application setApplicationIconBadgeNumber:0];// 去掉提示紅點
if(application.applicationState == UIApplicationStateActive){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您有新消息到達" message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] delegate:self cancelButtonTitle:@"忽略此消息" otherButtonTitles:@"現(xiàn)在查看", nil];
alert.tag = 666;
[alert show];
}else if(application.applicationState == UIApplicationStateInactive){
[[NSNotificationCenter defaultCenter] postNotificationName:@"PushNotification" object:nil];
}
}
#pragma mark - UIAlertView Delegate Methods
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag == 666 && buttonIndex == 1){
[[NSNotificationCenter defaultCenter] postNotificationName:@"PushNotification" object:nil];
}
}
然后在需要的頁面注冊通知,寫需要app執(zhí)行的方法
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。