<p id="qitwt"></p>
      1. <object id="qitwt"><strong id="qitwt"></strong></object>
        <p id="qitwt"></p>
        <li id="qitwt"></li>
        <p id="qitwt"><del id="qitwt"><xmp id="qitwt"></xmp></del></p>
        <p id="qitwt"><label id="qitwt"></label></p>
      2. <acronym id="qitwt"></acronym>
        <p id="qitwt"></p>
        免費開源的iOS開發學習平臺

        本地消息推送(基于iOS10 UserNotifications框架):4-UNUserNotificationCenterDelegate代理協議

        UNUserNotificationCenterDelegate是UNUserNotificationCenter類中定義的代理協議,其提供了當用戶對消息推送進行交互時可以調用的方法。

        UNUserNotificationCenterDelegate

        UNUserNotificationCenterDelegate中定義了如下兩個代理協議方法。

        • 當終端即將展示推送消息時,會調用willPresentNotification:方法
        - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;
        

        我們編寫了一個示例代碼,每60秒發送一次提醒,如下圖所示,不論用戶是否打開該推送消息,willPresentNotification:方法每60秒會被自動調用一次。

        • 當用戶點擊推送消息,打開App時, didReceiveNotificationResponse:方法會被調用
        - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
        

        如下圖所示,當用戶點擊推送消息時,didReceiveNotificationResponse:方法會被調用。

        獲取推送中攜帶的信息

        在UNUserNotificationCenterDelegate協議定義的代理方法中,可以獲取notification(UNNotification類)以及response(UNNotificationResponse類)參數,通過這兩個參數,我們都可以獲取到攜帶推送信息的content(UNNotificationContent類)對象。

        如下所示,我們可以通過response獲取content對象。

        -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
            NSLog(@"%s",__func__);
            //獲取**content**對象
            UNNotificationContent * content = response.notification.request.content;
            NSLog(@"notification body:%@",content.body);
            NSLog(@"notification subtitle:%@",content.subtitle);
            NSLog(@"notification title:%@",content.title);
            
        }
        

        注意點

        在實現UNUserNotificationCenterDelegate協議中的方法之前,不要忘記設置代理對象以及設置遵守代理協議。(以AppDelegate為例)

        • 設置遵守代理協議
        #import "AppDelegate.h"
        #import <UserNotifications/UserNotifications.h>
        @interface AppDelegate ()<UNUserNotificationCenterDelegate>
        @end
        
        • 設置代理對象
        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
            //設置代理對象
            center.delegate = self;
            return YES;
        }
        

        青青草国产成人久久_jizzjizz日本护士高清_亚洲 另类 国产 另类_99久久免费精品色老
        <p id="qitwt"></p>
          1. <object id="qitwt"><strong id="qitwt"></strong></object>
            <p id="qitwt"></p>
            <li id="qitwt"></li>
            <p id="qitwt"><del id="qitwt"><xmp id="qitwt"></xmp></del></p>
            <p id="qitwt"><label id="qitwt"></label></p>
          2. <acronym id="qitwt"></acronym>
            <p id="qitwt"></p>