您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 本地通知完整實作與五個必須知道的限制

編輯:iOS 開發綜合

本地通知不需要伺服器支援,由系統統一調度。下面從 AppDelegate 的完整生命週期切入,把註冊授權、建立通知、接收處理與取消的完整流程走一遍。

AppDelegate 實作

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 加入本地通知
    [self addLocalNotification];

    // 若 App 是由使用者點擊通知而啟動,可從 launchOptions 取出通知內容
    UILocalNotification *notification = [launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    NSDictionary *userInfo = notification.userInfo;

    NSLog(@"didFinishLaunchingWithOptions: The userInfo is %@.", userInfo);

    return YES;
}

#pragma mark - 建立本地通知

- (void)addLocalNotification
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];

    // 觸發時間:10 秒後
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10.0];

    // 重複間隔與參照日曆(使用前最好先設定時區,以便自動同步時間)
    notification.repeatInterval = 2;
    notification.repeatCalendar = [NSCalendar currentCalendar];

    // 通知內容
    notification.alertBody = @"最近添加了諸多有趣的特性,是否立即體驗?";
    notification.applicationIconBadgeNumber = 1;          // 圖示上的數字
    notification.alertAction = @"打開應用";                // 鎖屏上的滑動提示文字
    notification.alertLaunchImage = @"Default";           // 點擊通知開啟時的啟動圖

    // 自訂音效(需真機才能聽到,且檔案要放在 main bundle)
    notification.soundName = @"msg.caf";
    // notification.soundName = UILocalNotificationDefaultSoundName;

    // 附加資訊
    notification.userInfo = @{@"id" : @1, @"user" : @"Kenshin Cui"};

    // 排入通知佇列
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

#pragma mark - 使用者回應授權請求後

// 呼叫 registerUserNotificationSettings: 之後會執行這個方法
- (void)application:(UIApplication *)application
didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    if (notificationSettings.types != UIUserNotificationTypeNone)
    {
        [self addLocalNotification];
    }
}

#pragma mark - 生命週期

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // 回到前台時清除圖示上的數字
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

#pragma mark - 移除通知

- (void)removeNotification
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

#pragma mark - 接收通知

- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    NSLog(@"didReceiveLocalNotification: The userInfo is %@", userInfo);
}

@end

授權請求該放在哪裡?

原範例中有一段被條件編譯括起來的邏輯,值得提出來討論:

if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone)
{
    [self addLocalNotification];
}
else
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:
        [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |
                                                      UIUserNotificationTypeBadge |
                                                      UIUserNotificationTypeSound
                                            categories:nil]];
}

這個判斷的意思是:若尚未取得通知授權,就先請求授權,等使用者同意後再由 didRegisterUserNotificationSettings: 建立通知。這段邏輯本身是正確且值得參考的,但它有個重要前提——若從未發送過授權請求,系統的「設定」裡連通知的設定項目都不會出現,使用者也就無從手動開啟。因此首次啟動時主動請求授權是必要的。

同時也建議避免在啟動時立刻彈出授權視窗,比較好的做法是在使用者実際に觸發相關功能時(例如第一次設定提醒)再請求,通過率會高得多。

五個必須知道的限制

  1. 必須先註冊通知型別。使用者一旦拒絕,後續就無法再發送通知,除非他手動到 iOS 設定中開啟。
  2. 只在 App 位於背景或已關閉時才會收到。這是系統調度機制,前景時通知由 App 自己處理。這一點對遠程推送同樣適用。
  3. 音效格式有嚴格限制。必須是 Linear PCM、MA4(IMA/ADPCM)、μLaw 或 aLaw 之一,播放長度須在 30 秒內,否則會被系統音效取代。自訂音效檔必須放在 main bundle 中。
  4. 待處理通知最多 64 筆。超過這個數量的通知會被系統忽略,且新的會擠掉舊的。
  5. 可主動移除。用 cancelLocalNotification: 移除指定通知,或用 cancelAllLocalNotifications 一次清空。實務上建議在 userInfo 中帶自訂識別碼,以便精準移除。
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved