一、配置APNs
蘋果APNs(英文全稱:Apple Push Notification Service)
1.配置開發證書
![Uploading apns_13_803165.png …]
2.配置生產證書
和配置開發證書的流程相同。
3.將配置好的證書導出為 .p12 文件
一、注冊極光推送帳號
可登錄 極光推送官網 注冊帳號。
注冊成功後進入控制台
二、集成極光推送SDK
我使用的是JPush-iOS-SDK-2.1.0。下載的文件裡有個pdf文件iOS+SDK+Integration+Guide.pdf,介紹了集成極光推送的詳細代碼。
在工程文件中,做如下設置:
![Uploading apns_18_876090.png …]
下面是我寫的代碼:
AppDelegate.m文件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//設置appKey
[JPushConfig registerJPush:launchOptions];
return YES;
}
#pragma mark 修改通知處理函數
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
//推送消息攜帶 content-available: 1 是Background 運行的必須參數,如果不攜帶此字段則與iOS7 之前版本的普通推送一樣。
//清空角標
[JPUSHService resetBadge];
if (!userInfo) {
completionHandler(UIBackgroundFetchResultNoData);
return ;
}
completionHandler(UIBackgroundFetchResultNewData);
//根據服務器設置的 userInfo 的格式,進行解析
}
// Required
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[JPUSHService registerDeviceToken:deviceToken];
}
// Required
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[JPUSHService handleRemoteNotification:userInfo];
application.applicationIconBadgeNumber = 0;
}
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
[JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[application setApplicationIconBadgeNumber:0];
}
JPushConfig.m文件
/** 注冊 JPush */
+(void)registerJPush:(NSDictionary *)launchOptions {
// Required
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定義categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else {
//categories 必須為nil
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeBadge |
UIUserNotificationTypeBadge)
categories:nil];
}
NSString *appKey = @"極光推送生成的KEY";
NSString *channel = @"App Store";//發布渠道,可以隨便定義
BOOL isProduction = false;//這個要根據極光推送控制台裡設置的是開發環境還是生產環境
//Required
[JPUSHService setupWithOption:launchOptions
appKey:appKey
channel:channel
apsForProduction:isProduction];
}
收到通知消息後,在AppDelegate.m跳轉到指定界面的方法:
UIViewController *vc = [[UIViewController alloc] init];
[self.window.rootViewController presentViewController:vc animated:YES completion:nil];