iOS本地推送(本地通知)

前言

最近需要實現本地通知提醒功能,研究了下本地推送

通知實現

1.注冊通知

+ (void)registerLocalNotification:(NSInteger)alertTime string:(NSString *)string key:(NSString *)key{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    // 設置觸發通知的時間
    //需要使用時間戳
    NSDate *fireDate = [NSDate dateWithTimeIntervalSince1970:alertTime];
    NSLog(@"fireDate=%@",fireDate);
    notification.fireDate = fireDate;
    // 時區
    notification.timeZone = [NSTimeZone defaultTimeZone];
    // 設置重復的間隔
    notification.repeatInterval = 0;//0表示不重復
    // 通知內容
    notification.alertBody =  string;
    notification.applicationIconBadgeNumber = 1;
    // 通知被觸發時播放的聲音
    notification.soundName = UILocalNotificationDefaultSoundName;
    // 通知參數
    
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:string forKey:key];
    notification.userInfo = userDict;
    
    // ios8后,需要添加這個注冊,才能得到授權
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
                                                                                 categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        // 通知重復提示的單位,可以是天、周、月
        //        notification.repeatInterval = NSCalendarUnitDay;
    } else {
        // 通知重復提示的單位,可以是天、周、月
        //        notification.repeatInterval = NSDayCalendarUnit; //ios7使用
    }
    
    // 執行通知注冊
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

2.取消通知

+ (void)cancelLocalNotificationWithKey:(NSString *)key {
    // 獲取所有本地通知數組
    NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    
    for (UILocalNotification *notification in localNotifications) {
        NSDictionary *userInfo = notification.userInfo;
        if (userInfo) {
            // 根據設置通知參數時指定的key來獲取通知參數
            NSString *info = userInfo[key];
            
            // 如果找到需要取消的通知,則取消
            if (info != nil) {
                [[UIApplication sharedApplication] cancelLocalNotification:notification];
                break;
            }
        }
    }
}

3.在AppDelegate可以收到本地通知的回調,當然前提是你在程序內部

// 本地通知回調函數,當應用程序在前臺時調用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    NSLog(@"noti:%@",notification);
    [[NSNotificationCenter defaultCenter]postNotificationName:@"RefreshList" object:nil];
    // 更新顯示的徽章個數
    NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
    badge--;
    badge = badge >= 0 ? badge : 0;
    [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
}

注意

1.iOS系統沒有自定義時間間隔的通知,如果要實現類似功能需要注冊多個通知
2.如果重復次數為0的話,通知了一次這個通知就會從系統消除
3.如果你的通知沒有消除,即使卸載了程序,這依然會殘留,在下次裝入的時候會繼續運行,如果想要移除本地通知可以調用UIApplication的cancelLocalNotification:或cancelAllLocalNotifications移除指定通知或所有通知
4.在使用通知之前必須注冊通知類型,如果用戶不允許應用程序發送通知,則以后就無法發送通知,除非用戶手動到iOS設置中打開通知
5.通知的聲音是由iOS系統播放的,格式必須是Linear PCM、MA4(IMA/ADPCM)、μLaw、aLaw中的一種,并且播放時間必須在30s內,否則將被系統聲音替換,同時自定義聲音文件必須放到main boundle中
6.本地通知的數量是有限制的,最近的本地通知最多只能有64個,超過這個數量將被系統忽略

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

推薦閱讀更多精彩內容