iOS 本地推送

QiShare發表於2018-09-07

級別: ★★☆☆☆
標籤:「iOS推送」「iOSPush」「本地推送」
作者: dac_1033
審校: QiShare團隊

概述:
本地通知是由本地應用觸發的,是基於時間的通知形式,一般用於鬧鐘定時、待辦事項等提醒功能。

傳送本地通知的大體步驟如下:
(1)註冊本地通知;
(2)建立本地通知相關變數,並初始化;
(3)設定處理通知的時間fireDate;
(4)設定通知的內容:通知標題、通知聲音、圖示數字等;
(5)設定通知傳遞的引數userInfo,該字典內容可自定義(這一步可選);
(6)新增這個配置好的本地通知。

  1. 註冊本地通知
- (void)sendLocalNotification {
    
    NSString *title = @"通知-title";
    NSString *sutitle = @"通知-subtitle";
    NSString *body = @"通知-body";
    NSInteger badge = 1;
    NSInteger timeInteval = 5;
    NSDictionary *userInfo = @{@"id":@"LOCAL_NOTIFY_SCHEDULE_ID"};
    
    if (@available(iOS 10.0, *)) {
        // 1.建立通知內容
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
        [content setValue:@(YES) forKeyPath:@"shouldAlwaysAlertWhileAppIsForeground"];
        content.sound = [UNNotificationSound defaultSound];
        content.title = title;
        content.subtitle = subtitle;
        content.body = body;
        content.badge = @(badge);

        content.userInfo = userInfo;

        // 2.設定通知附件內容
        NSError *error = nil;
        NSString *path = [[NSBundle mainBundle] pathForResource:@"logo_img_02@2x" ofType:@"png"];
        UNNotificationAttachment *att = [UNNotificationAttachment attachmentWithIdentifier:@"att1" URL:[NSURL fileURLWithPath:path] options:nil error:&error];
        if (error) {
            NSLog(@"attachment error %@", error);
        }
        content.attachments = @[att];
        content.launchImageName = @"icon_certification_status1@2x";
        // 2.設定聲音
        UNNotificationSound *sound = [UNNotificationSound soundNamed:@"sound01.wav"];// [UNNotificationSound defaultSound];
        content.sound = sound;

        // 3.觸發模式
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInteval repeats:NO];

        // 4.設定UNNotificationRequest
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:LocalNotiReqIdentifer content:content trigger:trigger];

        // 5.把通知加到UNUserNotificationCenter, 到指定觸發點會被觸發
        [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        }];

    } else {
    
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        
        // 1.設定觸發時間(如果要立即觸發,無需設定)
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
        
        // 2.設定通知標題
        localNotification.alertBody = title;
        
        // 3.設定通知動作按鈕的標題
        localNotification.alertAction = @"檢視";
        
        // 4.設定提醒的聲音
        localNotification.soundName = @"sound01.wav";// UILocalNotificationDefaultSoundName;
        
        // 5.設定通知的 傳遞的userInfo
        localNotification.userInfo = userInfo;
        
        // 6.在規定的日期觸發通知
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        // 6.立即觸發一個通知
        //[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
    }
}
複製程式碼
  1. 取消本地通知
- (void)cancelLocalNotificaitons {
    
    // 取消一個特定的通知
    NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];
    // 獲取當前所有的本地通知
    if (!notificaitons || notificaitons.count <= 0) { return; }
    for (UILocalNotification *notify in notificaitons) {
        if ([[notify.userInfo objectForKey:@"id"] isEqualToString:@"LOCAL_NOTIFY_SCHEDULE_ID"]) {
            if (@available(iOS 10.0, *)) {
                [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[LocalNotiReqIdentifer]];
            } else {
                [[UIApplication sharedApplication] cancelLocalNotification:notify];
            }
            break;
        }
    }
    // 取消所有的本地通知
    //[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
複製程式碼

app請求通知使用者許可權的提示彈窗:

請求通知使用者許可權的提示彈窗

app切換到前、後臺或退出,還有鎖屏之後彈出通知的效果:

app切換到前臺時
app切換到後臺時
鎖屏

在上面的程式碼中我們設定了userInfo這個引數,在iOS中收到並點選通知,則會自動開啟當前應用。由於通知由系統排程那麼此時進入應用有兩種情況,如下:

  • 系統版本 < iOS10
// 如果App已經完全退出:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

// 當App已經完全退出時,獲取userInfo引數過程如下:
// NSDictionary *userInfoLocal = (NSDictionary *)[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
// NSDictionary *userInfoRemote = (NSDictionary *)[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

// 如果App還在執行(前臺or後臺)
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
複製程式碼
  • 系統版本 >= iOS10
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
#pragma mark - UNUserNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_PROHIBITED;
#endif
複製程式碼

備註:
(1)註冊通知型別,當使用者不允許時,則app無法接收通知;(使用者可以到iOS設定->通知->找到相應app,手動設定通知選項)
(2)遠端通知與本地通知是由iOS統一排程的,只有在應用退出到後臺或者關閉才能收到通知。
(3)通知的聲音是在程式碼中指定,由系統播放,播放時間必須在30s內,否則將被系統聲音替換,同時自定義聲音檔案必須放到main boundle中,本文Demo中使用wav格式聲音檔案。
(4)本地通知是有數量限制的,一般短時間內傳送的本地通知上限為64個,超過這個數量將被系統忽略(資料來源於網路,具體時間間隔待驗證)。

Demo原始碼:GitHub地址

關注我們的途徑有:
QiShare(簡書)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公眾號)

推薦文章:
iOS與JS互動之WKWebView-WKUIDelegate協議
如果360推出辣椒水,各位女士會買嗎?
奇舞週刊第 274 期: 秋風起,白雲飛

相關文章