iOS添加本地方法

添加本地通知需要進入項目的project->Capabilities把Push Notifications打開,并添加一下方法,注意ios8后,需要添加注冊,才能得到授權(quán)

// 設(shè)置本地通知
-(void)registerLocalNotificationWithTime:(NSTimeInterval)startTime notifiTitle:(NSString *)title notifiText:(NSString *)text key:(NSString *)key
{
    //避免重復(fù)添加
    [self cancelLocalNotificationWithKey:key];
    
    if (startTime <= 0)//返回的時間點小于當前時間,不彈
    {
    return;
    }
    

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    // 設(shè)置觸發(fā)通知的時間
    NSDate *fireDate = [NSDate dateWithTimeIntervalSince1970:startTime];
    fireDate = [NSDate dateWithTimeIntervalSinceNow:startTime];
    
    notification.fireDate = fireDate;
    // 時區(qū)
    notification.timeZone = [NSTimeZone defaultTimeZone];
    // 通知內(nèi)容
    notification.alertBody = text;
    notification.applicationIconBadgeNumber ++;
    // 通知被觸發(fā)時播放的聲音
    notification.soundName = UILocalNotificationDefaultSoundName;
    // 通知參數(shù)
    
    NSDictionary *notificationDic = @{@"key":key ?:@"", @"title":title?:@"",@"content":text?:@""};
    
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:notificationDic forKey:@"userInfo"];
    notification.userInfo = userDict;
    
    // ios8后,需要添加這個注冊,才能得到授權(quán)
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
                                                                                 categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        // 通知重復(fù)提示的單位,可以是天、周、月
//        notification.repeatInterval = NSCalendarUnitDay;
    } else {
        // 通知重復(fù)提示的單位,可以是天、周、月
//        notification.repeatInterval = NSDayCalendarUnit;
    }
    
    // 執(zhí)行通知注冊
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

在appDelegate中:

        UILocalNotification *localNotif =
        [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        if (localNotif) {
            
            NSDictionary *notDic = [localNotif.userInfo objectForKey:@"userInfo"];
            //    ASSERT_Class(notDic, NSDictionary);
            NSString *notKey = [notDic safe_stringForKey:@"key"];//直播 master_id
            self.liveKey = notKey;

            [__LiveManager jumptoNextPageWithMasterId:notKey vc:__lolita.topVC completeBlock:nil];
            // 更新顯示的徽章個數(shù)
            NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
            badge--;
            badge = badge >= 0 ? badge : 0;
            [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
        }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容