cocoa touch - Ask for User Permission to Receive UILocalNotifications in iOS 8 -
i have set local notifications in app delegate using this:
- (void)applicationdidenterbackground:(uiapplication *)application { uilocalnotification *notification = [[uilocalnotification alloc]init]; [notification setalertbody:@"watch latest episode of cca-tv"]; [notification setfiredate:[nsdate datewithtimeintervalsincenow:5]]; [notification settimezone:[nstimezone defaulttimezone]]; [application setscheduledlocalnotifications:[nsarray arraywithobject:notification]]; }
when run app , quit receive error saying:
2014-06-07 11:14:16.663 cca-tv[735:149070] attempting schedule local notification {fire date = saturday, june 7, 2014 @ 11:14:21 pacific daylight time, time zone = america/los_angeles (pdt) offset -25200 (daylight), repeat interval = 0, repeat count = uilocalnotificationinfiniterepeatcount, next fire date = saturday, june 7, 2014 @ 11:14:21 pacific daylight time, user info = (null)} with alert haven't received permission user display alerts
how can necessary permission display alerts?
since ios 8 need ask user's permission show notifications app, applies both remote/push , local notifications. in swift can this,
update swift 2.0
func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: nsdictionary?) -> bool { // override point customization after application launch. if(uiapplication.instancesrespondtoselector(selector("registerusernotificationsettings:"))) { let notificationcategory:uimutableusernotificationcategory = uimutableusernotificationcategory() notificationcategory.identifier = "invite_category" notificationcategory.setactions([replyaction], forcontext: uiusernotificationactioncontext.default) //registerting notification. application.registerusernotificationsettings(uiusernotificationsettings(fortypes:[.sound, .alert, .badge], categories: nil)) } else { //do ios 7 stuff, pretty nothing local notifications. } return true }
swift 3.2
if(uiapplication.instancesrespond(to: #selector(uiapplication.registerusernotificationsettings(_:)))){ let notificationcategory:uimutableusernotificationcategory = uimutableusernotificationcategory() notificationcategory.identifier = "invite_category" notificationcategory.setactions([replyaction], forcontext: uiusernotificationactioncontext.default) //registerting notification. application.registerusernotificationsettings(uiusernotificationsettings(types:[.sound, .alert, .badge], categories: nil)) } else{ //do ios 7 stuff, pretty nothing local notifications. }
objective c syntax similar.
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { if ([uiapplication instancesrespondtoselector:@selector(registerusernotificationsettings:)]){ [application registerusernotificationsettings:[uiusernotificationsettings settingsfortypes:uiusernotificationtypealert|uiusernotificationtypebadge|uiusernotificationtypesound categories:nil]]; } // override point customization after application launch. return yes; }
to check registered notification types can use uiapplication class's method,
- (uiusernotificationsettings *)currentusernotificationsettings
so if user has said no app function should return setting without types in it.
i have written tutorial this, see here.
Comments
Post a Comment