iOS开发 iOS10推送必看(基础篇)-转
<p>iOS10更新之后,推送也是做了一些小小的修改,下面我就给大家仔细说说。希望看完我的这篇文章,对大家有所帮助。</p><div>
<div>
<h2>一、简单入门篇---看完就可以简单适配完了</h2>
<blockquote>
<p>相对简单的推送证书以及环境的问题,我就不在这里讲啦,我在这里说的,是指原有工程的适配。</p>
</blockquote>
<p>1.首先我们需要打开下面的开关。所有的推送平台,不管是极光还是什么的,要想收到推送,这个是必须打开的哟~</p>
<div class="image-package">
<div class="image-container">
<div class="image-container-fill"> </div>
<div class="image-view" data-width="1123" data-height="251"><img src="//upload-images.jianshu.io/upload_images/707724-2f3b64b1c6cc3f82.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp" alt="" data-original-src="//upload-images.jianshu.io/upload_images/707724-2f3b64b1c6cc3f82.png" data-original-width="1123" data-original-height="251" data-original-format="image/png" data-original-filesize="45394"></div>
</div>
<div class="image-caption">QQ20160914-4.png</div>
</div>
<p>之后,系统会生成一个我们以前没见过的文件,如图:</p>
<br>
<div class="image-package">
<img src="http://static.zybuluo.com/coderXu/tkqx4rmo3utnm7i8blcu3f2l/QQ20160918-0.png" alt="QQ20160918-0.png-5.8kB" data-original-src="http://static.zybuluo.com/coderXu/tkqx4rmo3utnm7i8blcu3f2l/QQ20160918-0.png">
<div class="image-caption">QQ20160918-0.png-5.8kB</div>
</div>
<div class="image-package">
<img src="http://static.zybuluo.com/coderXu/7h5gea5k6uuqliw7tc7cwl5v/QQ20160918-1.png" alt="QQ20160918-1.png-26.3kB" data-original-src="http://static.zybuluo.com/coderXu/7h5gea5k6uuqliw7tc7cwl5v/QQ20160918-1.png">
<div class="image-caption">QQ20160918-1.png-26.3kB</div>
</div>
<p>可能产生的问题:之前有朋友反馈过,将开发环境由 <strong>development</strong> 变成 <strong>production</strong> ,在开关这里会产生错误,如图:<br>
</p>
<div class="image-package">
<img src="http://static.zybuluo.com/coderXu/y5l7m798wxu5499j30093487/QQ20160918-2.png" alt="QQ20160918-2.png-26.4kB" data-original-src="http://static.zybuluo.com/coderXu/y5l7m798wxu5499j30093487/QQ20160918-2.png">
<div class="image-caption">QQ20160918-2.png-26.4kB</div>
</div>
<p> </p>
<p>如果大家点击Fix issue之后,会惊奇的发现,APS Environment由 <strong>production</strong> 又变成 <strong>development</strong> 了。</p>
<h4>解决办法:我的建议是不做任何修改。</h4>
<p>经过我的测试,打包之后,生成的ipa包内,是没有这个.entitlements 文件的。经过测试,我发现是可以正常收到推送信息的。测试的方法如下,大家也可以测试一下。</p>
<h4>测试方法:打包之后安装ipa文件,然后利用极光推送,选择生产环境,推送,即可。</h4>
<p>经过上面的操作,你就会惊奇的发现,推送已经适配完毕了,iOS10的系统,已经可以正常接收通知了。</p>
<h2>二、中级篇</h2>
<blockquote>
<p>这里我会给大家讲一讲iOS10的推送,如何注册,通过什么代理,哪些方法可以用,哪些方法不可以用。</p>
</blockquote>
<h4>1.系统自带方法</h4>
<p>大家不管是使用三方平台的推送,还是系统自带的推送,都先应该了解下系统自带方法,如何实现远程通知的实现。</p>
<ul>
<li>第一步导入<span style="color: rgba(255, 0, 255, 1)">#import <UserNotifications/UserNotifications.h></span><br>
且要遵守<span style="color: rgba(255, 0, 255, 1)"><UNUserNotificationCenterDelegate></span>的协议,在Appdelegate.m中。<br>
这里需要注意,我们最好写成这种形式</li>
</ul>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">#ifdef</span><span style="color: rgba(0, 0, 0, 1)"> NSFoundationVersionNumber_iOS_9_x_Max
</span><span style="color: rgba(0, 0, 255, 1)">#import</span> <UserNotifications/UserNotifications.h>
<span style="color: rgba(0, 0, 255, 1)">#endif</span></pre>
</div>
<ul>
<li>第二步我们需要在 <span style="color: rgba(255, 0, 255, 1)"><code>(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions</code></span>中注册通知,代码如下:<br>
<div class="cnblogs_code">
<pre>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *<span style="color: rgba(0, 0, 0, 1)">)launchOptions {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> ([.systemVersion floatValue] >= <span style="color: rgba(128, 0, 128, 1)">10.0</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">iOS10特有</span>
UNUserNotificationCenter *center =<span style="color: rgba(0, 0, 0, 1)"> ;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 必须写代理,不然无法监听通知的接收与点击</span>
center.<span style="color: rgba(0, 0, 255, 1)">delegate</span> =<span style="color: rgba(0, 0, 0, 1)"> self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert </span>| UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError *<span style="color: rgba(0, 0, 0, 1)"> _Nullable error) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (granted) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 点击允许</span>
NSLog(<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">注册成功</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
[center getNotificationSettingsWithCompletionHandler:</span>^(UNNotificationSettings *<span style="color: rgba(0, 0, 0, 1)"> _Nonnull settings) {
NSLog(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">%@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, settings);
}];
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 点击不允许</span>
NSLog(<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">注册失败</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
}
}];
}</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> ([.systemVersion floatValue] ><span style="color: rgba(128, 0, 128, 1)">8.0</span><span style="color: rgba(0, 0, 0, 1)">){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">iOS8 - iOS10</span>
];
}</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> ([.systemVersion floatValue] < <span style="color: rgba(128, 0, 128, 1)">8.0</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">iOS8系统以下</span>
;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 注册获得device Token</span>
[ registerForRemoteNotifications];</pre>
</div>
</li>
</ul>
<p>其中,获得Device Token的方法是没有改变的。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获得Device Token</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span>)application:(UIApplication *<span style="color: rgba(0, 0, 0, 1)">)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData </span>*<span style="color: rgba(0, 0, 0, 1)">)deviceToken {
NSLog(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">%@</span><span style="color: rgba(128, 0, 0, 1)">"</span>, );
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获得Device Token失败</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span>)application:(UIApplication *<span style="color: rgba(0, 0, 0, 1)">)application
didFailToRegisterForRemoteNotificationsWithError:(NSError </span>*<span style="color: rgba(0, 0, 0, 1)">)error {
NSLog(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">did Fail To Register For Remote Notifications With Error: %@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, error);
}</span></pre>
</div>
<p>此次iOS10系统的更新,苹果给了我们2个代理方法来处理通知的接收和点击事件,这两个方法在<UNUserNotificationCenterDelegate>的协议中,大家可以查看下。此外,苹果把本地通知跟远程通知合二为一。区分本地通知跟远程通知的类是UNPushNotificationTrigger.h类中,UNPushNotificationTrigger的类型是新增加的,通过它,我们可以得到一些通知的触发条件,在使用时,我们不应该直接使用这个类,应当使用它的子类。</p>
<ul>
<li>
<p>我简单点说</p>
</li>
<li>
<p>1.<span style="color: rgba(255, 0, 255, 1)">UNPushNotificationTrigger</span> (远程通知) 远程推送的通知类型</p>
</li>
<li>
<p>2.<span style="color: rgba(255, 0, 255, 1)">UNTimeIntervalNotificationTrigger</span> (本地通知) 一定时间之后,重复或者不重复推送通知。我们可以设置timeInterval(时间间隔)和repeats(是否重复)。</p>
</li>
<li>
<p>3.<span style="color: rgba(255, 0, 255, 1)">UNCalendarNotificationTrigger</span>(本地通知) 一定日期之后,重复或者不重复推送通知 例如,你每天8点推送一个通知,只要dateComponents为8,如果你想每天8点都推送这个通知,只要repeats为YES就可以了。</p>
</li>
<li>
<p>4.<span style="color: rgba(255, 0, 255, 1)">UNLocationNotificationTrigger</span> (本地通知)地理位置的一种通知,<br>
当用户进入或离开一个地理区域来通知。在CLRegion标识符必须是唯一的。因为如果相同的标识符来标识不同区域的UNNotificationRequests,会导致不确定的行为。</p>
</li>
</ul>
<p>接收通知的代码如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> iOS 10收到通知</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span>)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(<span style="color: rgba(0, 0, 255, 1)">void</span> (^<span style="color: rgba(0, 0, 0, 1)">)(UNNotificationPresentationOptions options))completionHandler{
NSDictionary </span>* userInfo =<span style="color: rgba(0, 0, 0, 1)"> notification.request.content.userInfo;
UNNotificationRequest </span>*request = notification.request; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 收到推送的请求</span>
UNNotificationContent *content = request.content; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 收到推送的消息内容</span>
NSNumber *badge = content.badge;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 推送消息的角标</span>
NSString *body = content.body; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 推送消息体</span>
UNNotificationSound *sound = content.sound;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 推送消息的声音</span>
NSString *subtitle = content.subtitle;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 推送消息的副标题</span>
NSString *title = content.title;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 推送消息的标题</span>
<span style="color: rgba(0, 0, 255, 1)">if</span>(]) {
NSLog(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">iOS10 前台收到远程通知:%@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, );
}
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断为本地通知</span>
NSLog(<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">iOS10 前台收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,body,title,subtitle,badge,sound,userInfo);
}
completionHandler(UNNotificationPresentationOptionBadge</span>|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置</span>
}</pre>
</div>
<p>下面的代码则是通知的点击事件:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 通知的点击事件</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span>)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(<span style="color: rgba(0, 0, 255, 1)">void</span>(^<span style="color: rgba(0, 0, 0, 1)">)())completionHandler{
NSDictionary </span>* userInfo =<span style="color: rgba(0, 0, 0, 1)"> response.notification.request.content.userInfo;
UNNotificationRequest </span>*request = response.notification.request; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 收到推送的请求</span>
UNNotificationContent *content = request.content; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 收到推送的消息内容</span>
NSNumber *badge = content.badge;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 推送消息的角标</span>
NSString *body = content.body; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 推送消息体</span>
UNNotificationSound *sound = content.sound;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 推送消息的声音</span>
NSString *subtitle = content.subtitle;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 推送消息的副标题</span>
NSString *title = content.title;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 推送消息的标题</span>
<span style="color: rgba(0, 0, 255, 1)">if</span>(]) {
NSLog(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">iOS10 收到远程通知:%@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, );
}
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断为本地通知</span>
NSLog(<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">iOS10 收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,body,title,subtitle,badge,sound,userInfo);
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.</span>
completionHandler();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 系统要求执行这个方法</span>
<span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p>在点击事件中,如果我们不写<code>completionHandler()</code>这个方法,可能会报一下的错误,希望大家注意下~<br>
<code>Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.</code></p>
<p>最后最后,我们要大家补充一下,旧版本的一些方法,方便大家扩充iOS10的通知的通知,不影响原有逻辑。</p>
<div class="cnblogs_code">
<pre>- (<span style="color: rgba(0, 0, 255, 1)">void</span>)application:(UIApplication *<span style="color: rgba(0, 0, 0, 1)">)application
didReceiveRemoteNotification:(NSDictionary </span>*<span style="color: rgba(0, 0, 0, 1)">)userInfo {
NSLog(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">iOS6及以下系统,收到通知:%@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, );
}
</span>- (<span style="color: rgba(0, 0, 255, 1)">void</span>)application:(UIApplication *<span style="color: rgba(0, 0, 0, 1)">)application
didReceiveRemoteNotification:(NSDictionary </span>*<span style="color: rgba(0, 0, 0, 1)">)userInfo
fetchCompletionHandler:
(</span><span style="color: rgba(0, 0, 255, 1)">void</span> (^<span style="color: rgba(0, 0, 0, 1)">)(UIBackgroundFetchResult))completionHandler {
NSLog(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">iOS7及以上系统,收到通知:%@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, );
completionHandler(UIBackgroundFetchResultNewData);
}</span></pre>
</div>
<h4>2.极光推送(需要下载最新的版本)</h4>
<blockquote>
<p>如果用到三方的一些平台,做推送就会更为简单。</p>
</blockquote>
<p>1.注册通知的代码如下</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">if</span> ([.systemVersion floatValue] >= <span style="color: rgba(128, 0, 128, 1)">10.0</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">#ifdef</span><span style="color: rgba(0, 0, 0, 1)"> NSFoundationVersionNumber_iOS_9_x_Max
JPUSHRegisterEntity </span>* entity =<span style="color: rgba(0, 0, 0, 1)"> [ init];
entity.types </span>= UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|<span style="color: rgba(0, 0, 0, 1)">UNAuthorizationOptionSound;
;
</span><span style="color: rgba(0, 0, 255, 1)">#endif</span><span style="color: rgba(0, 0, 0, 1)">
} </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> ([.systemVersion floatValue] >= <span style="color: rgba(128, 0, 128, 1)">8.0</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">可以添加自定义categories</span>
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |<span style="color: rgba(0, 0, 0, 1)">
UIUserNotificationTypeSound </span>|<span style="color: rgba(0, 0, 0, 1)">
UIUserNotificationTypeAlert)
categories:nil];
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">categories 必须为nil</span>
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |<span style="color: rgba(0, 0, 0, 1)">
UIRemoteNotificationTypeSound </span>|<span style="color: rgba(0, 0, 0, 1)">
UIRemoteNotificationTypeAlert)
categories:nil];
}</span></pre>
</div>
<p>注册完成之后,我们则需要加入极光推送更新后,新加入的2个方法,这两个方法在<JPUSHRegisterDelegate>代理方法中。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
* @brief handle UserNotifications.framework
* @param center 新特性用户通知中心
* @param notification 前台得到的的通知对象
* @param completionHandler 该callback中的options 请使用UNNotificationPresentationOptions
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span>)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(<span style="color: rgba(0, 0, 255, 1)">void</span> (^<span style="color: rgba(0, 0, 0, 1)">)(NSInteger options))completionHandler;
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
* @brief handle UserNotifications.framework
* @param center 新特性用户通知中心
* @param response 通知响应对象
* @param completionHandler
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span>)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(<span style="color: rgba(0, 0, 255, 1)">void</span>(^)())completionHandler;</pre>
</div>
<p> </p>
<p>使用时,只需要在上面的代码中添加极光的处理方法就可以了,具体使用如下图:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">if</span>(]) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 这个方法,不管是收到通知代理还是点击通知的代理,如果使用极光推送,我们都是需要增加这个方法的。</span>
<span style="color: rgba(0, 0, 0, 1)"> ;
NSLog(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">iOS10 收到远程通知:%@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, );
;
}
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断为本地通知</span>
NSLog(<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">iOS10 收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,body,title,subtitle,badge,sound,userInfo);
}</span></pre>
</div>
<p>通过上面的文章,相信大家已经可以初步了解新版本的推送,要如何处理啦~</p>
<p>稍后,我会更新以下的内容,希望大家支持:<br>
UNNotificationContentExtension - 通知内容扩展<br>
UNNotificationServiceExtension- 通知服务扩展<br>
UNNotificationAction - 通知响应的方法<br>
UNErrorCode - 通知错误<br>
UNNotificationAttachment - 附件通知<br>
稍后我还会上传demo路径。</p>
</div>
<br>作者:徐不同<br>来源:简书</div><br><br>
来源:https://www.cnblogs.com/jiuyi/p/10940040.html
頁:
[1]