IOS 10 Add local push (locally Notification)

Source: Internet
Author: User

Objective

The Uilocalnotification (UIKit framework) is deprecated in IOS 10, with a new usernotifications framework to push notifications, and the push notification has its own label UN (this treatment is really others), as well as a series of enhancements to the push feature (two extension and interface experience optimizations), are Apple's pro-son, so pushing this part of the feature has also become a focus in development.

This article mainly review the iOS 10 related documents, sorted out the local push notification under iOS 10, because it is code, do not do more explanation, directly read the code and comments, there is a question message discussion oh.

New push registration mechanism

Registration Notice (APPDELEGATE.M):

#import <UserNotifications/UserNotifications.h> #import "AppDelegate.h" @interface appdelegate () < Unusernotificationcenterdelegate> @end @implementation appdelegate-(BOOL) Application: (UIApplication *) Application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {//Use Unusernotificationcenter to manage notifications UNUse    Rnotificationcenter *center = [Unusernotificationcenter currentnotificationcenter];        Listener Callback Event center.delegate = self; IOS 10 is registered with the following methods to be authorized [center Requestauthorizationwithoptions: (Unauthorizationoptionalert +                              Unauthorizationoptionsound) completionhandler:^ (BOOL Granted, Nserror * _nullable error) {                          Enable or disable features based on authorization.        }]; Gets the current notification settings, Unnotificationsettings is a read-only object, cannot be modified directly, and can only be obtained through the following methods [center Getnotificationsettingswithcompletionhandler    : ^ (unnotificationsettings * _nonnull settings) {}]; return YES;} #pragma mark-unusernotificationcenterdelegate//is processed before the notification is displayed, which means that you have the opportunity to modify the notification before displaying the notice. -(void) Usernotificationcenter: (unusernotificationcenter *) Center willpresentnotification: (unnotification *) Notification Withcompletionhandler: (void (^) (unnotificationpresentationoptions)) completionhandler{//1. Handling Notifications//2 . After processing is completed, a completionhandler is used to indicate the form Completionhandler (Unnotificationpresentationoptionalert) to display the notification in the foreground. @end
Push local notifications
Use unnotification local notification + (void) RegisterNotification: (Nsinteger) alertime{//Use Unusernotificationcenter to manage notifications U        nusernotificationcenter* Center = [Unusernotificationcenter Currentnotificationcenter];    Create a Unmutablenotificationcontent object that contains the content to be notified, and note that it is not a Unnotificationcontent object that is immutable.    unmutablenotificationcontent* content = [[Unmutablenotificationcontent alloc] init];    Content.title = [NSString localizedusernotificationstringforkey:@ "hello!" arguments:nil];    Content.body = [NSString localizedusernotificationstringforkey:@ "Hello_message_body" arguments:nil];        Content.sound = [Unnotificationsound defaultsound]; Push local push untimeintervalnotificationtrigger* after alerttime trigger = [Untimeintervalnotificationtrigger triggerWithTime    Interval:alertime Repeats:no]; unnotificationrequest* request = [unnotificationrequest requestwithidentifier:@ "Fivesecond" Content:content Trigger:        Trigger];    Add the processing after successful push! [Center Addnotificationrequest:request WITHCompletionhandler:^ (Nserror * _nullable error) {Uialertcontroller *alert = [Uialertcontroller Alertcontrollerwithti        tle:@ "Local Notification" message:@ "successfully added push" preferredstyle:uialertcontrollerstylealert];        Uialertaction *cancelaction = [uialertaction actionwithtitle:@ "Cancel" Style:uialertactionstylecancel Handler:nil];        [Alert addaction:cancelaction]; [[UIApplication Sharedapplication].keywindow.rootviewcontroller Presentviewcontroller:alert Animated:YES    Completion:nil]; }];}

Pre-local push notifications for IOS 10:

+ (void) Registerlocalnotificationinoldway: (Nsinteger) Alerttime {///iOS8, you need to add this registration to get authorization//if ([[UIApplication sha Redapplication] Respondstoselector: @selector (registerusernotificationsettings:)]) {//Uiusernotificationtype type = Uiusernotificationtypealert | Uiusernotificationtypebadge |    Uiusernotificationtypesound;    Uiusernotificationsettings *settings = [uiusernotificationsettings settingsfortypes:type//Categories:nil];    [[UIApplication sharedapplication] registerusernotificationsettings:settings];    Notification of the unit of repeated prompts, can be days, weeks, months//} uilocalnotification *notification = [[Uilocalnotification alloc] init];    Set the time to trigger the notification nsdate *firedate = [NSDate datewithtimeintervalsincenow:alerttime];        NSLog (@ "firedate=%@", firedate);    Notification.firedate = firedate;    Time zone Notification.timezone = [Nstimezone defaulttimezone];        Set the repetition interval notification.repeatinterval = Kcfcalendarunitsecond; Notification content notification.alertbody = @ "It's time to get up.".";    Notification.applicationiconbadgenumber = 1;    The sound that is played when the notification is triggered notification.soundname = Uilocalnotificationdefaultsoundname;    Notification parameters Nsdictionary *userdict = [Nsdictionary dictionarywithobject:@ "started learning iOS developed" forkey:@ "key"];        Notification.userinfo = userdict; After iOS8, you need to add this registration to be authorized if ([[UIApplication sharedapplication] Respondstoselector: @selector ( Registerusernotificationsettings:)] {uiusernotificationtype type = Uiusernotificationtypealert | Uiusernotificationtypebadge |        Uiusernotificationtypesound;                                                                                 Uiusernotificationsettings *settings = [Uiusernotificationsettings settingsfortypes:type        Categories:nil];        [[UIApplication sharedapplication] registerusernotificationsettings:settings];    Notice the unit of repeated prompts, can be days, weeks, months notification.repeatinterval = Nscalendarunitday; } else {//notification repeating prompt unit, can be day, week, month Notification.repeatinterval = NsdaycalendarUnit; }//Perform notification registration [[uiapplication sharedapplication] schedulelocalnotification:notification];}

IOS 10 Add local push (locally Notification)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.