欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

ios(推送)之本地推送 - 新聞資訊 - 云南小程序開發(fā)|云南軟件開發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開發(fā)/軟件開發(fā)

知識(shí)

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們在追求其視覺表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!

您當(dāng)前位置>首頁 » 新聞資訊 » 技術(shù)分享 >

ios(推送)之本地推送

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

發(fā)布人:葵宇科技

瀏覽次數(shù):41


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

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

然后,在具體的viewcontroller中實(shí)現(xiàn)推送:
- (IBAction)localPushNow:(id)sender {
 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //本地推送
        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];
            
        }
    });

}

在appdelegate中會(huì)接收到推送信息:

//接收本地推送
//接收本地推送
- (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];
}

過程中可能會(huì)出現(xiàn)如下狀況:
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
可能是因?yàn)槟銢]有注冊,或者設(shè)置中沒有開啟推送功能,

相關(guān)案例查看更多