iOS 通知機制總結

iOS中提供了2種推送通知

  • 本地推送通知(Local Notification)
  • 遠程推送通知(Remote Notification)

推送通知的作用

  • 可以讓不在前臺運行的app,告知用戶app內部發生了什么事情
  • 推送通知的呈現效果總結
  • 用戶接收的推送通知,都會展示在“通知中心”
  • 從屏幕頂部往下滑,就能調出“通知中心”
  • 顯示橫幅還是UIAlertView,取決于用戶的設置

總結一下,推送通知有5種不同的呈現效果

  • 在屏幕頂部顯示一塊橫幅(顯示具體內容)
  • 在屏幕中間彈出一個UIAlertView(顯示具體內容)
  • 在鎖屏界面顯示一塊橫幅(鎖屏狀態下,顯示具體內容)
  • 更新app圖標的數字(說明新內容的數量)
  • 播放音效(提醒作用)

** 推送通知的使用細節**

  • 發出推送通知時,如果當前程序正運行在前臺,那么推送通知就不會被呈現出來
  • 點擊推送通知后,默認會自動打開發出推送通知的app
  • 不管app打開還是關閉,推送通知都能如期發出

本地推送

AppDelegate.m

#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;
}
/** 
* 點擊通知打開應用的時候會執行該方法 
* 應用在前臺的時候,收到通知也會執行該方法 
* 
* @param 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]
    label.font = [UIFont systemFontOfSize:14];
    label.numberOfLines = 0;
    [self.window.rootViewController.view addSubview:label];
   // if (application.applicationState == UIApplicationStateBackground) {
// 
// }
}
- (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 {
 /* 
 @property(nonatomic,copy) NSDate *fireDate;
 @property(nonatomic,copy) NSTimeZone *timeZone;
 @property(nonatomic) NSCalendarUnit repeatInterval;
 @property(nonatomic,copy) NSCalendar *repeatCalendar;
 @property(nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0);
 @property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0);
 @property(nonatomic,copy) NSString *alertBody;
 @property(nonatomic) BOOL hasAction;
 @property(nonatomic,copy) NSString *alertAction;
 @property(nonatomic,copy) NSString *alertLaunchImage;
 @property(nonatomic,copy) NSString *soundName;
 UILocalNotificationDefaultSoundName @property(nonatomic) NSInteger applicationIconBadgeNumber;
 @property(nonatomic,copy) NSDictionary *userInfo;
 */
     // 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 = @"1111";
     // 1.6.設置啟動的音效
     localNote.soundName = UILocalNotificationDefaultSoundName;
     // 1.7.設置應用圖標提醒的數字
     localNote.applicationIconBadgeNumber = 999;
     // 1.8.如果想將通知的信息傳遞過去,必須使用userInfo屬性
     localNote.userInfo = @{@"msg" : @"吃飯了嗎", @"date" : localNote.fireDate};
     // 2.調度通知
     [[UIApplication sharedApplication] scheduleLocalNotification:localNote];
     [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
- (IBAction)removeLocalNote {
     [[UIApplication sharedApplication] cancelAllLocalNotifications];
// [UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)
}
@end

遠程推送

** AppDelegate.m**

#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);
}

/**
 *  當接受到遠程通知的時候會調用該方法
 *
 *  @param userInfo    遠程通知的信息
 */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    // 在這里可以跳轉的其他頁面
    NSLog(@"%@", userInfo);
}

/**
 *  如果接受到遠程通知時,想要后臺執行任務,則實現調用該方法
 *
 *  @param userInfo
 *  @param completionHandler 后臺執行完之后要告知系統,是否更新成功
 */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSLog(@"%@", userInfo);
    
    completionHandler(UIBackgroundFetchResultNewData);
}

@end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 極光推送: 1.JPush當前版本是1.8.2,其SDK的開發除了正常的功能完善和擴展外也緊隨蘋果官方的步伐,SD...
    Isspace閱讀 6,792評論 10 16
  • 推送通知注意:這里說的推送通知跟NSNotification有所區別NSNotification是抽象的,不可見的...
    醉葉惜秋閱讀 1,547評論 0 3
  • 概述 在多數移動應用中任何時候都只能有一個應用程序處于活躍狀態,如果其他應用此刻發生了一些用戶感興趣的那么通過通知...
    莫離_焱閱讀 6,564評論 1 8
  • 推送通知 注意:這里說的推送通知跟NSNotification有所區別 NSNotification是抽象的,不可...
    iOS開發攻城獅閱讀 4,346評論 1 13
  • (來到新部門,努力了將近一年的網站,今天,終于上線了,微信也同步更新了,一塊大石頭終于落地,所以我把寫給學生們的推...
    愛君如初閱讀 346評論 0 0