ios(推送)之(zhī)本地(dì / de)推送 - 新聞資訊 - 雲南小程序開發|雲南軟件開發|雲南網站建設-昆明融晨信息技術有限公司

159-8711-8523

雲南網建設/小程序開發/軟件開發

知識

不(bù)管是(shì)網站,軟件還是(shì)小程序,都要(yào / yāo)直接或間接能爲(wéi / wèi)您産生價值,我們在(zài)追求其視覺表現的(de)同時(shí),更側重于(yú)功能的(de)便捷,營銷的(de)便利,運營的(de)高效,讓網站成爲(wéi / wèi)營銷工具,讓軟件能切實提升企業内部管理水平和(hé / huò)效率。優秀的(de)程序爲(wéi / wèi)後期升級提供便捷的(de)支持!

您當前位置>首頁 » 新聞資訊 » 技術分享 >

ios(推送)之(zhī)本地(dì / de)推送

發表時(shí)間:2021-1-4

發布人(rén):融晨科技

浏覽次數:41


iOS上(shàng)有兩種消息通知,一種是(shì)本地(dì / de)消息(Local Notification),一種是(shì)遠程消息(Push Notification,也(yě)叫Remote Notification),設計這(zhè)兩種通知的(de)目的(de)都是(shì)爲(wéi / wèi)了(le/liǎo)提醒用戶,現在(zài)有些什麽新鮮的(de)事情發生了(le/liǎo),吸引用戶重新打開應用。本地(dì / de)推送也(yě)可以(yǐ)通過服務器控制,比如說(shuō)如果有新消息了(le/liǎo),推送消息,但是(shì),前提是(shì)程序必須是(shì)打開的(de),而(ér)遠程推送,是(shì)通過蘋果APNS服務器,推送給手機,手機在(zài)推送給具體的(de)哪個(gè)程序,一般遠程推送用到(dào)的(de)比較多,先介紹下本地(dì / de)推送,下節在(zài)介紹遠程推送。
本地(dì / de)推送:
首先,先在(zài)appdelegate中注冊:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];//注冊本地(dì / de)推送
    
    // Override point for customization after application launch.
    return YES;
}

然後,在(zài)具體的(de)viewcontroller中實現推送:
- (IBAction)localPushNow:(id)sender {
 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //本地(dì / de)推送
        UILocalNotification*notification = [[UILocalNotification alloc]init];
        NSDate * pushDate = [NSDate dateWithTimeIntervalSinceNow:10];
        if (notification != nil) {
            notification.fireDate = pushDate;
            notification.timeZone = [NSTimeZone defaultTimeZone];
            notification.repeatInterval = kCFCalendarUnitDay;
            notification.soundName = UILocalNotificationDefaultSoundName;
            notification.alertBody = @"hello,world";
            notification.applicationIconBadgeNumber = 0;
            NSDictionary*info = [NSDictionary dictionaryWithObject:@"test" forKey:@"name"];
            notification.userInfo = info;
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
            
        }
    });

}

在(zài)appdelegate中會接收到(dào)推送信息:

//接收本地(dì / de)推送
//接收本地(dì / de)推送
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    NSLog(@"%@",notification.alertBody);
    UILabel*label = [[UILabel alloc]init];
    label.frame = CGRectMake(0, 0, 160, 20);
    label.layer.cornerRadius = 10;
    label.backgroundColor = [UIColor blackColor];
    label.text = notification.alertBody;
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
    
    [self.window addSubview:label];
}

過程中可能會出(chū)現如下狀況:
Attempting to schedule a local notification……with a sound but haven't received permission from the user to play sounds
Attempting to schedule a local notification……with an alert but haven't received permission from the user to display alerts
可能是(shì)因爲(wéi / wèi)你沒有注冊,或者設置中沒有開啓推送功能,

相關案例查看更多