知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們在追求其視覺表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
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)案例查看更多
相關(guān)閱讀
- 云南網(wǎng)站建設(shè)服務(wù)公司
- web
- 小程序開發(fā)排名前十名
- 網(wǎng)站小程序
- 做小程序被騙
- 微信小程序開發(fā)入門課程
- 定制小程序開發(fā)
- 昆明小程序公司
- 云南網(wǎng)站建設(shè)方法
- 小程序開發(fā)費(fèi)用
- 云南網(wǎng)站建設(shè)開發(fā)
- 云南網(wǎng)站建設(shè) 網(wǎng)絡(luò)服務(wù)
- 海報(bào)插件
- 網(wǎng)站建設(shè)方法
- 云南做軟件
- 手機(jī)網(wǎng)站建設(shè)
- 網(wǎng)站優(yōu)化
- 云南網(wǎng)站建設(shè)案例
- 小程序商城
- 云南省城鄉(xiāng)建設(shè)廳網(wǎng)站
- 支付寶小程序被騙
- 報(bào)廢車
- 汽車報(bào)廢管理
- 網(wǎng)站排名
- 人口普查小程序
- 云南小程序代建
- 網(wǎng)站建設(shè)方案 doc
- 網(wǎng)站優(yōu)化哪家好
- 云南小程序開發(fā)公司推薦
- 云南網(wǎng)站建設(shè)首選