iOS 推送通知

iOS 推送通知:


注意:通知是一個應用程序級別的操作UIApplication

推送通知 NSNotification 區別

NSNotification是抽象的,不可見的

推送通知是可見的


iOS中提供了2種推送通知

本地推送通知(Local Notification

遠程推送通知(Remote Notification


推送通知的作用: 可以讓不在前臺運行的app,告知用戶app內部發生了什麼事情


推送通知的呈現效果總結

用戶接收的推送通知,都會展示在通知中心

從屏幕頂部往下滑,就能調出通知中心

顯示橫幅還是UIAlertViewController,取決於用戶的設置


推送通知5種不同的呈現效果

在屏幕頂部顯示一塊橫幅(顯示具體內容:發送者和消息)

在屏幕中間彈出一個UIAlertView(顯示具體內容)

在鎖屏界面顯示一塊橫幅(鎖屏狀態下,顯示具體內容)

更新app圖標的數字(新內容的數量)

播放音效(提醒作用)


推送通知的使用細節

發出推送通知時,如果當前程序正運行在前臺,那麼推送通知就不會被呈現出來

點擊推送通知後,默認會自動打開發出推送通知的app

不管app打開還是關閉,推送通知都能如期發出


本地推送

AppDelegate.h文件

#import "AppDelegate.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    

    /*

     UIUserNotificationTypeNone    = 0,      不給用戶發通知

     UIUserNotificationTypeBadge   = 1 << 0, 是否可以改變應用圖標右上角的提示數字

     UIUserNotificationTypeSound   = 1 << 1, 通知聲音

     UIUserNotificationTypeAlert   = 1 << 2, 是否有彈窗提示

     */

    if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {

        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];

        [application registerUserNotificationSettings:settings];

    }

    

    if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {

        // 跳轉

        UILabel *label = [[UILabel alloc] init];

        label.frame = CGRectMake(0, 300, 300, 300);

        label.backgroundColor = [UIColor redColor];

        label.text = [NSString stringWithFormat:@"%@", launchOptions];

        label.font = [UIFont systemFontOfSize:14];

        //自動換行

        label.numberOfLines = 0;

        [self.window.rootViewController.view addSubview:label];

    }

    

    return YES;

}


/**

 *  點擊通知打開應用的時候會執行該方法

 *  應用在前臺的時候,收到通知也會執行該方法

 *

 *   notification 通知

 */

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

{

    UILabel *label = [[UILabel alloc] init];

    label.frame = CGRectMake(0, 0, 300, 300);

    label.backgroundColor = [UIColor redColor];

    label.text = [NSString stringWithFormat:@"notification--%@", notification];

    label.font = [UIFont systemFontOfSize:14];

    label.numberOfLines = 0;

    [self.window.rootViewController.view addSubview:label];

    

}


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

{

    return YES;

}


@end

推送

#import "ViewController.h"


@interface ViewController ()


/**

 *  點擊按鈕後添加本地通知

 */

- (IBAction)addLocalNote;

/**

 *  移除通知---不常用

 */

- (IBAction)removeLocalNote;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

}


/**

 *  點擊按鈕後添加本地通知

 */

- (IBAction)addLocalNote {

    

   

    // 1.創建一個本地通知

    UILocalNotification *localNote = [[UILocalNotification alloc] init];

    

    // 1.1.設置通知發出的時間

    localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

    

    // 1.2.設置通知發出的內容

    localNote.alertBody = @"你好麼?";

    

    // 1.3.是否彈出提示框

    localNote.hasAction = YES;

    

    // 1.4.設置提示框

    localNote.alertAction = @"請查看";

    

    // 1.5.設置啓動的圖片

    localNote.alertLaunchImage = [UIImage imageNamed:@"beijing"];

    

    // 1.6.設置啓動的音效

    localNote.soundName = UILocalNotificationDefaultSoundName;

    

    // 1.7.設置應用圖標提醒的數字

    localNote.applicationIconBadgeNumber = 20;

    

    // 1.8.如果想將通知的信息傳遞過去,必須使用userInfo屬性,切爲一個字典類型

    localNote.userInfo = @{@"msg" : @"你好麼", @"date" : localNote.fireDate};

    

    // 調度通知

    [[UIApplication sharedApplication] scheduleLocalNotification:localNote];

    

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

}


- (IBAction)removeLocalNote {

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    //    [UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)

}

@end

遠程推送


1.appDelegate.h文件

#import "AppDelegate.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    

    if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {

        // 1.向用戶請求可以給用戶推送消息

        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];

        [application registerUserNotificationSettings:settings];

        

        // 2.註冊遠程通知

        //首先拿到用戶的DeviceToken

        [application registerForRemoteNotifications];

    } else {

        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];

    }

    

//    if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {

//        // 頁面的跳轉

//    }

    

    [application setApplicationIconBadgeNumber:0];

    

    return YES;

}


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{

    // 將用戶的用戶名和deviceToken發送給服務器,讓服務器進行保存備份即可

    NSLog(@"%@", deviceToken);

}


/**

 *  當接受到遠程通知的時候會調用該方法

 */

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

    //userInfo    遠程通知的信息

    // 在這裏跳轉

    NSLog(@"%@", userInfo);

}


/**

 *  如果接受到遠程通知時,想要後臺執行任務,則實現調用該方法

 *

 *  @param userInfo

 *  @param completionHandler 後臺執行完之後要告知系統,是否更新成功

 */

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

{

    NSLog(@"%@", userInfo);

    

    completionHandler(UIBackgroundFetchResultNewData);

}


@end



發佈了41 篇原創文章 · 獲贊 4 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章