iOS10 注册极光推送

2024-01-31 11:32
文章标签 注册 推送 ios10 极光

本文主要是介绍iOS10 注册极光推送,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先, 在极光的开发者服务里注册应用获取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 TargetCapabilities->Push Notifications选项

这两个一定要都是对号 ,  这个选项不开启在iOS10后不会注册成功


添加这个选项会在项目中多这样一个文件



5. 不要忘记Xcode7以上需要支持http传输方式



下面是需要写的代码部分:

6. 在AppDelegate.m中, 引入头文件

[objc]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. // 极光推送  
  2. #import "JPUSHService.h"  
  3. #import <AdSupport/AdSupport.h>  
  4. #ifdef NSFoundationVersionNumber_iOS_9_x_Max  
  5. #import <UserNotifications/UserNotifications.h> // 这里是iOS10需要用到的框架  
  6. #endif  


7. 设置注册极光推送需要的一些参数

[objc]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. static NSString * const JPUSHAPPKEY = @"xxxxxxxxxxxxxxxxx"// 极光appKey  
  2. static NSString * const channel = @"Publish channel"// 固定的  
  3.   
  4. #ifdef DEBUG // 开发  
  5.   
  6. static BOOL const isProduction = FALSE; // 极光FALSE为开发环境  
  7.   
  8. #else // 生产  
  9.   
  10. static BOOL const isProduction = TRUE; // 极光TRUE为生产环境  
  11.   
  12. #endif  

8. 这里是AppDelegate.m中的代码, 分了几大块, 全部粘到下面, 直接复制可用(只需要下面这些代码就可以实现通知)

[objc]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  AppDelegate.m  
  3. //  iOS10_JPUSH  
  4. //  
  5. //  Created by 周昊 on 16/9/18.  
  6. //  Copyright © 2016年 周昊. All rights reserved.  
  7. //  
  8.   
  9. #import "AppDelegate.h"  
  10. // 极光推送  
  11. #import "JPUSHService.h"  
  12. #import <AdSupport/AdSupport.h>  
  13. #ifdef NSFoundationVersionNumber_iOS_9_x_Max  
  14. #import <UserNotifications/UserNotifications.h> // 这里是iOS10需要用到的框架  
  15. #endif  
  16.   
  17. static NSString * const JPUSHAPPKEY = @"xxxxxxxxxxxxxxxxx"// 极光appKey  
  18. static NSString * const channel = @"Publish channel"// 固定的  
  19.   
  20. #ifdef DEBUG // 开发  
  21.   
  22. static BOOL const isProduction = FALSE; // 极光FALSE为开发环境  
  23.   
  24. #else // 生产  
  25.   
  26. static BOOL const isProduction = TRUE; // 极光TRUE为生产环境  
  27.   
  28. #endif  
  29.   
  30. @interface AppDelegate ()<JPUSHRegisterDelegate> // 最新版的sdk需要实现这个代理方法  
  31.   
  32. @end  
  33.   
  34. @implementation AppDelegate  
  35.   
  36.   
  37. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  38.       
  39.     // 注册apns通知  
  40.     if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0// iOS10  
  41.     {  
  42. #ifdef NSFoundationVersionNumber_iOS_9_x_Max  
  43.         JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];  
  44.         entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge | UNAuthorizationOptionSound;  
  45.         [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];  
  46. #endif  
  47.     }  
  48.     else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0// iOS8, iOS9  
  49.     {  
  50.         //可以添加自定义categories  
  51.         [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];  
  52.     }  
  53.     else // iOS7  
  54.     {  
  55.         //categories 必须为nil  
  56.         [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];  
  57.     }  
  58.     /* 
  59.      *  launchingOption 启动参数. 
  60.      *  appKey 一个JPush 应用必须的,唯一的标识. 
  61.      *  channel 发布渠道. 可选. 
  62.      *  isProduction 是否生产环境. 如果为开发状态,设置为 NO; 如果为生产状态,应改为 YES. 
  63.      *  advertisingIdentifier 广告标识符(IDFA) 如果不需要使用IDFA,传nil. 
  64.      * 此接口必须在 App 启动时调用, 否则 JPush SDK 将无法正常工作. 
  65.      */  
  66.     // 广告标识符  
  67.     NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];  
  68.       
  69.     // 如不需要使用IDFA,advertisingIdentifier 可为nil  
  70.     // 注册极光推送  
  71.     [JPUSHService setupWithOption:launchOptions appKey:JPUSHAPPKEY channel:channel apsForProduction:isProduction advertisingIdentifier:advertisingId];  
  72.       
  73.     //2.1.9版本新增获取registration id block接口。  
  74.     [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {  
  75.         if(resCode == 0)  
  76.         {  
  77.             // iOS10获取registrationID放到这里了, 可以存到缓存里, 用来标识用户单独发送推送  
  78.             NSLog(@"registrationID获取成功:%@",registrationID);  
  79.             [[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@"registrationID"];  
  80.             [[NSUserDefaults standardUserDefaults] synchronize];  
  81.         }  
  82.         else  
  83.         {  
  84.             NSLog(@"registrationID获取失败,code:%d",resCode);  
  85.         }  
  86.     }];  
  87.       
  88.     return YES;  
  89. }  
  90.   
  91. // ---------------------------------------------------------------------------------  
  92. - (void)applicationWillResignActive:(UIApplication *)application {  
  93. }  
  94.   
  95.   
  96. - (void)applicationDidEnterBackground:(UIApplication *)application {  
  97.     [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];  
  98. }  
  99.   
  100.   
  101. - (void)applicationWillEnterForeground:(UIApplication *)application {  
  102.     [application setApplicationIconBadgeNumber:0];  
  103.     [application cancelAllLocalNotifications];  
  104. }  
  105.   
  106.   
  107. - (void)applicationDidBecomeActive:(UIApplication *)application {  
  108. }  
  109.   
  110.   
  111. - (void)applicationWillTerminate:(UIApplication *)application {  
  112. }  
  113.   
  114. // ---------------------------------------------------------------------------------  
  115. #pragma mark - 注册推送回调获取 DeviceToken  
  116. #pragma mark -- 成功  
  117. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken  
  118. {  
  119.     // 注册成功  
  120.     // 极光: Required - 注册 DeviceToken  
  121.     [JPUSHService registerDeviceToken:deviceToken];  
  122. }  
  123.   
  124. #pragma mark -- 失败  
  125. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error  
  126. {  
  127.     // 注册失败  
  128.     NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);  
  129. }  
  130.   
  131. // ---------------------------------------------------------------------------------  
  132.   
  133. // 这部分是官方demo里面给的, 也没实现什么功能, 放着以备不时之需  
  134. #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1  
  135. - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings  
  136. {  
  137.       
  138. }  
  139.   
  140. // Called when your app has been activated by the user selecting an action from  
  141. // a local notification.  
  142. // A nil action identifier indicates the default action.  
  143. // You should call the completion handler as soon as you've finished handling  
  144. // the action.  
  145. - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler  
  146. {  
  147.       
  148. }  
  149.   
  150. // Called when your app has been activated by the user selecting an action from  
  151. // a remote notification.  
  152. // A nil action identifier indicates the default action.  
  153. // You should call the completion handler as soon as you've finished handling  
  154. // the action.  
  155. - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler  
  156. {  
  157.       
  158. }  
  159. #endif  
  160.   
  161. // ---------------------------------------------------------------------------------  
  162. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification  
  163. {  
  164.     [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];  
  165. }  
  166.   
  167. // ---------------------------------------------------------------------------------  
  168.   
  169. #pragma mark - iOS7: 收到推送消息调用  
  170. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {  
  171.       
  172.     // iOS7之后调用这个  
  173.     [JPUSHService handleRemoteNotification:userInfo];  
  174.     NSLog(@"iOS7及以上系统,收到通知");  
  175.       
  176.     if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0 || application.applicationState > 0)  
  177.     {  
  178.         // 程序在前台或通过点击推送进来的会弹这个alert  
  179.         NSString *message = [NSString stringWithFormat:@"iOS7-8-9收到的推送%@", [userInfo[@"aps"] objectForKey:@"alert"]];  
  180.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];  
  181.         [alert show];  
  182.     }  
  183.     completionHandler(UIBackgroundFetchResultNewData);  
  184. }  
  185.   
  186. // ---------------------------------------------------------------------------------  
  187.   
  188. #pragma mark - iOS10: 收到推送消息调用(iOS10是通过Delegate实现的回调)  
  189. #pragma mark- JPUSHRegisterDelegate  
  190. #ifdef NSFoundationVersionNumber_iOS_9_x_Max  
  191. // 当程序在前台时, 收到推送弹出的通知  
  192. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {  
  193.       
  194.     NSDictionary * userInfo = notification.request.content.userInfo;  
  195.       
  196.     if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])  
  197.     {  
  198.         [JPUSHService handleRemoteNotification:userInfo];  
  199.         NSString *message = [NSString stringWithFormat:@"will%@", [userInfo[@"aps"] objectForKey:@"alert"]];  
  200.         NSLog(@"iOS10程序在前台时收到的推送: %@", message);  
  201.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];  
  202.         [alert show];  
  203.     }  
  204.   
  205.     completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置  
  206. }  
  207.   
  208.   
  209. // 程序关闭后, 通过点击推送弹出的通知  
  210. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {  
  211.       
  212.     NSDictionary * userInfo = response.notification.request.content.userInfo;  
  213.       
  214.     if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])  
  215.     {  
  216.         [JPUSHService handleRemoteNotification:userInfo];  
  217.         NSString *message = [NSString stringWithFormat:@"did%@", [userInfo[@"aps"] objectForKey:@"alert"]];  
  218.         NSLog(@"iOS10程序关闭后通过点击推送进入程序弹出的通知: %@", message);  
  219.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];  
  220.         [alert show];  
  221.     }  
  222.    
  223.     completionHandler();  // 系统要求执行这个方法  
  224. }  
  225. #endif  
  226.   
  227. @end  

这篇关于iOS10 注册极光推送的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/663620

相关文章

Spring 源码解读:自定义实现Bean定义的注册与解析

引言 在Spring框架中,Bean的注册与解析是整个依赖注入流程的核心步骤。通过Bean定义,Spring容器知道如何创建、配置和管理每个Bean实例。本篇文章将通过实现一个简化版的Bean定义注册与解析机制,帮助你理解Spring框架背后的设计逻辑。我们还将对比Spring中的BeanDefinition和BeanDefinitionRegistry,以全面掌握Bean注册和解析的核心原理。

Chapter 13 普通组件的注册使用

欢迎大家订阅【Vue2+Vue3】入门到实践 专栏,开启你的 Vue 学习之旅! 文章目录 前言一、组件创建二、局部注册三、全局注册 前言 在 Vue.js 中,组件是构建应用程序的基本单元。本章详细讲解了注册和使用 Vue 的普通组件的两种方式:局部注册和全局注册。 本篇文章参考黑马程序员 一、组件创建 ①定义 Vue 组件是一种具有特定功能的 Vue 实

c++11工厂子类实现自注册的两种方法

文章目录 一、产品类构建1. 猫基类与各品种猫子类2.狗基类与各品种狗子类 二、工厂类构建三、客户端使用switch-case实现调用不同工厂子类四、自注册方法一:公开注册函数显式注册五、自注册方法二:构造函数隐形注册总结 一、产品类构建 1. 猫基类与各品种猫子类 class Cat {public:virtual void Printer() = 0;};class

Android 友盟消息推送集成遇到的问题

友盟消息推送遇到的问题 集成友盟消息推送,步骤根据提供的技术文档接入便可。可是当你集成到项目中去的时候,可能并不是一帆风顺就搞定,因为你项目里面是可能集成了其他的sdk(比如支付宝,微信,七鱼等等三方的sdk)。那么这个时候,再加上友盟的消息推送sdk集成可能就会出现问题。 问题清单 友盟消息推送sdk和支付宝sdk冲突问题 后台配置了消息推送,也显示发送成功,但是手机没有收到消息通知

SAP学习笔记 - 开发02 - BTP实操流程(账号注册,BTP控制台,BTP集成开发环境搭建)

上一章讲了 BAPI的概念,以及如何调用SAP里面的既存BAPI。 SAP学习笔记 - 开发01 - BAPI是什么?通过界面和ABAP代码来调用BAPI-CSDN博客 本章继续讲开发相关的内容,主要就是BTP的实际操作流程,比如账号注册,登录,BTP集成开发环境的搭建这方面。 目录 1,账号注册 2,BTP登录URL 3,如何在BTP上进行开发? 以下是详细内容。 1,账

Redis应用之Feed流关注推送

我的博客大纲 我的后端学习大纲 -------------------------------------------------------------------------------------------------------------------------------------------------# 3.好友关注: 3.1.关注和取关: a.接口说明:

吐血整理nacos 作为springcloud的配置中心和注册中心

吐血整理nacos 作为配置中心和注册中心 环境版本nacos 版本 nacos启动单机模式启动配置数据库 Spring cloud 连接注册Nacos配置中心导入依赖 注册中心 环境版本 SpringBoot版本SpringCloud版本cloud Alibaba版本2.6.132021.0.52021.0.5.0 参照依据 spring-cloud-alibab 对应

【中国国际航空-注册/登录安全分析报告】

前言 由于网站注册入口容易被黑客攻击,存在如下安全问题: 1. 暴力破解密码,造成用户信息泄露 2. 短信盗刷的安全问题,影响业务及导致用户投诉 3. 带来经济损失,尤其是后付费客户,风险巨大,造成亏损无底洞 所以大部分网站及App 都采取图形验证码或滑动验证码等交互解决方案, 但在机器学习能力提高的当下,连百度这样的大厂都遭受攻击导致点名批评, 图形验证及交互验证方式的安全性到底如

GIT-macOS配置推送代码到GitHub

文章目录 前言1. **设置 HTTP/HTTPS**临时设置(仅对当前会话有效)永久设置(写入配置文件) 2. **设置 SOCKS **3. **取消设置**4. **验证配置**5. **测试推送** 总结 前言 macOS配置推送代码到GitHub 1. 设置 HTTP/HTTPS 临时设置(仅对当前会话有效) git config --global ht