iOS10 注册极光推送
来源:互联网 发布:6g以下的单机游戏知乎 编辑:程序博客网 时间:2024/05/17 04:46
首先, 在极光的开发者服务里注册应用获取appKey, 在apple Developer配置推送证书...等等等这些废话就不说了.
兼容iOS10的是极光2.1.9版本的sdk.
1. 导入SDK
2. 导入SDK依赖的系统框架
CFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
Xcode7需要的是libz.tbd ; Xcode7以下版本是libz.dylib
Adsupport.framework (获取IDFA需要;如果不使用IDFA,请不要添加)
UserNotifications.framework(Xcode8及以上)
3. 设置Build Setting中, Search Paths的User Header Search Paths
4. 如果用的是Xcode8及以上环境开发需要开启Application Target的Capabilities->Push Notifications选项
这两个一定要都是对号 , 这个选项不开启在iOS10后不会注册成功
添加这个选项会在项目中多这样一个文件
5. 不要忘记Xcode7以上需要支持http传输方式
下面是需要写的代码部分:
6. 在AppDelegate.m中, 引入头文件
-
- #import "JPUSHService.h"
- #import <AdSupport/AdSupport.h>
- #ifdef NSFoundationVersionNumber_iOS_9_x_Max
- #import <UserNotifications/UserNotifications.h> // 这里是iOS10需要用到的框架
- #endif
7. 设置注册极光推送需要的一些参数- static NSString * const JPUSHAPPKEY = @"xxxxxxxxxxxxxxxxx";
- static NSString * const channel = @"Publish channel";
-
- #ifdef DEBUG // 开发
-
- static BOOL const isProduction = FALSE;
-
- #else // 生产
-
- static BOOL const isProduction = TRUE;
-
- #endif
8. 这里是AppDelegate.m中的代码, 分了几大块, 全部粘到下面, 直接复制可用(只需要下面这些代码就可以实现通知)
-
-
-
-
-
-
-
-
- #import "AppDelegate.h"
-
- #import "JPUSHService.h"
- #import <AdSupport/AdSupport.h>
- #ifdef NSFoundationVersionNumber_iOS_9_x_Max
- #import <UserNotifications/UserNotifications.h> // 这里是iOS10需要用到的框架
- #endif
-
- static NSString * const JPUSHAPPKEY = @"xxxxxxxxxxxxxxxxx";
- static NSString * const channel = @"Publish channel";
-
- #ifdef DEBUG // 开发
-
- static BOOL const isProduction = FALSE;
-
- #else // 生产
-
- static BOOL const isProduction = TRUE;
-
- #endif
-
- @interface AppDelegate ()<JPUSHRegisterDelegate>
-
- @end
-
- @implementation AppDelegate
-
-
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
-
-
- 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)
- {
-
- [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
- }
- else
- {
-
- [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
- }
-
-
-
-
-
-
-
-
-
- NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
-
-
-
- [JPUSHService setupWithOption:launchOptions appKey:JPUSHAPPKEY channel:channel apsForProduction:isProduction advertisingIdentifier:advertisingId];
-
-
- [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
- if(resCode == 0)
- {
-
- NSLog(@"registrationID获取成功:%@",registrationID);
- [[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@"registrationID"];
- [[NSUserDefaults standardUserDefaults] synchronize];
- }
- else
- {
- NSLog(@"registrationID获取失败,code:%d",resCode);
- }
- }];
-
- return YES;
- }
-
-
- - (void)applicationWillResignActive:(UIApplication *)application {
- }
-
-
- - (void)applicationDidEnterBackground:(UIApplication *)application {
- [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
- }
-
-
- - (void)applicationWillEnterForeground:(UIApplication *)application {
- [application setApplicationIconBadgeNumber:0];
- [application cancelAllLocalNotifications];
- }
-
-
- - (void)applicationDidBecomeActive:(UIApplication *)application {
- }
-
-
- - (void)applicationWillTerminate:(UIApplication *)application {
- }
-
-
- #pragma mark - 注册推送回调获取 DeviceToken
- #pragma mark -- 成功
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
- {
-
-
- [JPUSHService registerDeviceToken:deviceToken];
- }
-
- #pragma mark -- 失败
- - (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 forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
- {
-
- }
-
-
-
-
-
-
- - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
- {
-
- }
- #endif
-
-
- - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
- {
- [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
- }
-
-
-
- #pragma mark - iOS7: 收到推送消息调用
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
-
-
- [JPUSHService handleRemoteNotification:userInfo];
- NSLog(@"iOS7及以上系统,收到通知");
-
- if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0 || application.applicationState > 0)
- {
-
- NSString *message = [NSString stringWithFormat:@"iOS7-8-9收到的推送%@", [userInfo[@"aps"] objectForKey:@"alert"]];
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];
- [alert show];
- }
- completionHandler(UIBackgroundFetchResultNewData);
- }
-
-
-
- #pragma mark - iOS10: 收到推送消息调用(iOS10是通过Delegate实现的回调)
- #pragma mark- JPUSHRegisterDelegate
- #ifdef NSFoundationVersionNumber_iOS_9_x_Max
-
- - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
-
- NSDictionary * userInfo = notification.request.content.userInfo;
-
- if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])
- {
- [JPUSHService handleRemoteNotification:userInfo];
- NSString *message = [NSString stringWithFormat:@"will%@", [userInfo[@"aps"] objectForKey:@"alert"]];
- NSLog(@"iOS10程序在前台时收到的推送: %@", message);
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];
- [alert show];
- }
-
- completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
- }
-
-
-
- - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
-
- NSDictionary * userInfo = response.notification.request.content.userInfo;
-
- if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])
- {
- [JPUSHService handleRemoteNotification:userInfo];
- NSString *message = [NSString stringWithFormat:@"did%@", [userInfo[@"aps"] objectForKey:@"alert"]];
- NSLog(@"iOS10程序关闭后通过点击推送进入程序弹出的通知: %@", message);
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];
- [alert show];
- }
-
- completionHandler();
- }
- #endif
-
- @end
0 0