iOS 注册极光推送
注冊極光推送
一.在 AppDelegate.h 文件中定義appKey、channel、isProduction三個字段
#import <UIKit/UIKit.h>
static NSString *appKey = @"XXXXXXXXXX";
static NSString *channel = @"Publish channel";
static BOOL isProduction = FALSE;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
二.在 AppDelegate.m 文件中添加 極光推送的頭文件 和 iOS 10.0 以上系統版本的 <UserNotifications/UserNotifications.h>
#import "JPUSHService.h"
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
#import <UserNotifications/UserNotifications.h>
#endif
三.添加代理方法
@interface AppDelegate ()<UNUserNotificationCenterDelegate,JPUSHRegisterDelegate>
四.注冊極光推送
- (void)registJPushWithlaunchOptions:(NSDictionary *)launchOptions{
?? ?
? ? //3.0.0以前版本舊的注冊方式
? ? if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
? ? ? ? JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
? ? ? ? entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
? ? ? ? [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
#endif
? ? } else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
? ? ? ? //可以添加自定義categories
? ? ? ? [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? UIUserNotificationTypeSound |
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? UIUserNotificationTypeAlert)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? categories:nil];
? ? }
?? ?
? ? //如不需要使用IDFA,advertisingIdentifier 可為nil
? ? [JPUSHService setupWithOption:launchOptions appKey:appKey
? ? ? ? ? ? ? ? ? ? ? ? ? channel:channel
?? ? ? ? ? ? ? ? apsForProduction:isProduction
? ? ? ? ? ? advertisingIdentifier:nil];
?? ?
? ? //2.1.9版本新增獲取registration id block接口。
? ? [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
? ? ? ? if(resCode == 0){
//? ? ? ? ? ? NSLog(@"registrationID獲取成功:%@",registrationID);
?? ? ? ? ? ?
? ? ? ? }
? ? ? ? else{
//? ? ? ? ? ? NSLog(@"registrationID獲取失敗,code:%d",resCode);
? ? ? ? }
? ? }];
?? ?
}
//注冊成功,獲取deviceToken
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
?? ?
//? ? NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]);
? ? [JPUSHService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//? ? NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
- (void)application:(UIApplication *)application
didRegisterUserNotificationSettings:
(UIUserNotificationSettings *)notificationSettings {
}
- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)userInfo
? completionHandler:(void (^)())completionHandler {
}
#endif
+ (void)registrationIDCompletionHandler:(void(^)(int resCode,NSString *registrationID))completionHandler{
?? ?
}
總結
以上是生活随笔為你收集整理的iOS 注册极光推送的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascrip --- 构造函数的
- 下一篇: 用计算机画好看的图形,如何画一手漂亮的电