推送通知-設置類別 - (Obj-C)

iOS 8.0支持設置推送通知的類別,執行一些快捷操作:

action_1.png
action_2.png
action_3.png
action_4.png

彈窗型效果:

彈窗型_1.png

點擊"選項":


彈窗型_2.png

橫幅型效果:

橫幅型.png

點擊前臺就會打開應用,點擊后臺按鈕,后臺運行狀態

需要注意的是,如果設置了類別, 只有當執行類別中的某個動作時, 才會執行新的回調方法, 以及當App處于前臺時仍然會調用老方法(didReceiveLocalNotification)

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0) __TVOS_PROHIBITED;

方法,需要使用下面的方法接收推送通知的信息,即便程序退出,也是可以收到推送通知的

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
/**
 *  當接收到使用iOS 8 動作類別的通知后調用
 *
 *  @param application       應用
 *  @param identifier        動作的標識符
 *  @param notification      通知
 *  @param completionHandler 完成回調
 */
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{
    
    if ([identifier isEqualToString:@"forceground"]) { // 點擊前臺按鈕
        NSLog(@"點擊前臺按鈕");
    } else {
        NSLog(@"點擊后臺按鈕");
    }
    
    // 不調用回調控制臺會彈出警告
    // 作用:系統需要根據回調來更新應用的快照&設置應用的掛起 前臺模式會進入應用直接刷新了UI,而后臺模式執行時,為了提升用戶體驗,需要更新快照
    completionHandler();
    
}

如果沒有調用完成回調,控制臺會彈出警告:

Warning: Application delegate received call to -application:handleActionWithIdentifier:forLocalNotification:completionHandler: but the completion handler was never called.

執行回調的作用:系統需要根據回調來更新應用的快照&設置應用的掛起
原因:前臺模式會進入應用直接刷新了UI,而后臺模式執行時,為了提升用戶體驗,需要更新快照

切換應用時的每一個視圖都是一個應用快照:


快照.png

示例代碼:

ViewController:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

// 創建本地通知
- (IBAction)StartLocalNoteButtonClick:(id)sender {
    
    // 創建本地通知
    UILocalNotification *localNotification = [[UILocalNotification alloc]init];
    
    // 設置屬性
    // 1.觸發事件
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    // 2.設置通知的顯示內容
    localNotification.alertBody = @"收到一條新消息";
    
    // 設置通知的類別  (與類別進行綁定)
    localNotification.category = @"localNote";
    
    // 設置傳遞的信息
    localNotification.userInfo = @{@"page":@1};
    
    // 預定通知(UIApplication 作為系統和應用的橋梁,一般負責應用和系統相關的工作)
    // iOS 8.0以后,在預定和展示通知之前必須要注冊通知,請求授權
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 注冊通知,請求授權
    /*
         UIUserNotificationTypeNone  無任何效果
         UIUserNotificationTypeBadge 通知含有角標
         UIUserNotificationTypeSound 帶聲音
         UIUserNotificationTypeAlert 提示文字
     */
    
    // 設置類別
    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc]init];
    // 設置類別標記
    category.identifier = @"localNote";
    
    // 創建動作
    UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc]init];
    // 設置標識符
    action1.identifier = @"foreground";
    // 設置標題
    action1.title = @"我是前臺按鈕";
    // 設置激活模式
    action1.activationMode = UIUserNotificationActivationModeForeground;
    
    // 創建動作
    UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc]init];
    action2.identifier = @"background";
    action2.title = @"我是后臺按鈕";
    action2.activationMode = UIUserNotificationActivationModeBackground;
    
    // 添加動作
    /*
         UIUserNotificationActionContextDefault  設置該類型 彈窗型最多可以展示6個按鈕(4個自定義按鈕,2個默認按鈕:打開和關閉)
         UIUserNotificationActionContextMinimal  設置該類型 相當于設置iOS7沒有自定義按鈕的樣式(打開和關閉,如果屏幕較小展示不夠,可以設置此類型)
     */
    [category setActions:@[action1,action2] forContext:UIUserNotificationActionContextDefault];
    
    UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:[NSSet setWithObjects:category, nil]];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
    
}

@end

AppDelegate:

/**
 *  當接收到使用iOS 8 動作類別的通知后調用
 *
 *  @param application       應用
 *  @param identifier        動作的標識符
 *  @param notification      通知
 *  @param completionHandler 完成回調
 */
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{
    
    if ([identifier isEqualToString:@"forceground"]) { // 點擊前臺按鈕
        NSLog(@"點擊前臺按鈕");
    } else {
        NSLog(@"點擊后臺按鈕");
    }
    
    // 不調用回調控制臺會彈出警告
    // 作用:系統需要根據回調來更新應用的快照&設置應用的掛起 前臺模式會進入應用直接刷新了UI,而后臺模式執行時,為了提升用戶體驗,需要更新快照
    completionHandler();
    
}

UIMutableUserNotificationAction屬性介紹:

     
// 動作的標識符  用于區分不同的通知動作(按鈕/滑塊)
@property (nullable, nonatomic, copy) NSString *identifier;
     
// 動作上的文字
@property (nullable, nonatomic, copy) NSString *title;
     
// 行為 iOS9行為
@property (nonatomic, assign) UIUserNotificationActionBehavior behavior NS_AVAILABLE_IOS(9_0);
     
// 行為的參數
@property (nonatomic, copy) NSDictionary *parameters NS_AVAILABLE_IOS(9_0);
     
// 激活模式
@property (nonatomic, assign) UIUserNotificationActivationMode activationMode;
     
UIUserNotificationActivationModeForeground, // 點擊按鈕后讓APP進入前臺
UIUserNotificationActivationModeBackground  // 點擊按鈕后讓APP進入后臺
     
     
// 鎖屏時打開通知是否需要解鎖
@property (nonatomic, assign, getter=isAuthenticationRequired) BOOL authenticationRequired;
     
// 是否具有毀滅性  設置該按鈕為紅色
@property (nonatomic, assign, getter=isDestructive) BOOL destructive;

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

推薦閱讀更多精彩內容

  • 寫作原因:網上看了很多推送文章都沒有完美的解答我的疑惑;主要有以下兩點,1:推送來了我點擊應用圖標進入應用怎么取到...
    Thebloodelves閱讀 4,567評論 26 71
  • 極光推送: 1.JPush當前版本是1.8.2,其SDK的開發除了正常的功能完善和擴展外也緊隨蘋果官方的步伐,SD...
    Isspace閱讀 6,783評論 10 16
  • 前言 本文是一篇轉載文章,在這一篇實用的文章里,你可以按照上面的步驟實現不借助第三方和服務器端,自己給自己的設備發...
    進無盡閱讀 1,694評論 6 6
  • 版權聲明 本文翻譯自:raywenderlich.com 原文作者: Jack Wu 譯者: JMStack 轉載...
    jmstack閱讀 7,267評論 6 30
  • 應用程序必須進行適當配置,才可以接受本地或遠程通知。配置過程在iOS和OS X略有不同,但基本原理是相同的。在啟動...
    shenzhenboy閱讀 1,415評論 1 2