网红有一腿 發表於 2019-5-21 11:28:00

iOS开发之注册推送通知权限

<p>1、首先在工程设置开启通知权限(Push Notifications开关打开)</p>
<p><img src="https://img2018.cnblogs.com/blog/1347820/201905/1347820-20190521112357390-1099843882.png" alt=""></p>
<p>&nbsp;</p>
<p>2、注册通知权限,在此步骤会弹出用户授权提示</p>
<div class="cnblogs_code">
<pre></pre>
<p>#import &lt;UserNotifications/UserNotifications.h&gt;</p>
<pre><span style="color: rgba(0, 0, 255, 1)"><br>if</span> ([.systemVersion floatValue] &gt;= <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)">if</span> (@available(iOS <span style="color: rgba(128, 0, 128, 1)">10.0</span>, *<span style="color: rgba(0, 0, 0, 1)">)) {
            UNUserNotificationCenter </span>*center =<span style="color: rgba(0, 0, 0, 1)"> ;
            __weak </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(self) weakSelf =<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) {
                  [center getNotificationSettingsWithCompletionHandler:</span>^(UNNotificationSettings *<span style="color: rgba(0, 0, 0, 1)"> _Nonnull settings) {
                        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (settings.authorizationStatus ==<span style="color: rgba(0, 0, 0, 1)"> UNAuthorizationStatusAuthorized){
                            dispatch_async(dispatch_get_main_queue(), </span>^<span style="color: rgba(0, 0, 0, 1)">{
                              [ registerForRemoteNotifications];
                            });
                        }
                  }];
                }
            }];
      }
    } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> ([.systemVersion floatValue] &gt;= <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, 0, 255, 1)">if</span> (@available(iOS <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, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> ([ respondsToSelector:@selector(registerUserNotificationSettings:)]) {
                UIUserNotificationSettings</span>* notificationSettings = ;
                [ registerUserNotificationSettings:notificationSettings];
                [ registerForRemoteNotifications];
            } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                [ registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge </span>| UIRemoteNotificationTypeSound |<span style="color: rgba(0, 0, 0, 1)"> UIRemoteNotificationTypeAlert)];
            }
      }
    }</span></pre>
</div>
<p>注意iOS8.0和iOS10.0之后需要用不同的方法</p>
<p>3、获取到远程推送的token值</p>
<p>在Appdelegate类中重写下面的方法</p>
<div class="cnblogs_code">
<pre>- (<span style="color: rgba(0, 0, 255, 1)">void</span>)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *<span style="color: rgba(0, 0, 0, 1)">)deviceToken
{
    NSString </span>*token = ;
    token </span>= ;
    token </span>= ;
    token </span>= ;
}<br></span></pre>
</div>
<p>&nbsp;获取到token值之后妥善保存,并传给服务器,服务器根据token值就可以通过APNS给app发送远程推送通知了啦。</p><br><br>
来源:https://www.cnblogs.com/hecanlin/p/10898783.html
頁: [1]
查看完整版本: iOS开发之注册推送通知权限