IOS10 Push Notification adaptation

Source: Internet
Author: User

IOS10 pushes the new usernotifications Framework, which is actually very simple to use.

Just click on the notification bar on the iOS10 above system, the callback method will not go the original two methods

-(void) Application: (UIApplication *) application didreceiveremotenotification: (Nsdictionary *) UserInfo {}-(void) Application: (UIApplication *) application didreceiveremotenotification: ( Nsdictionary *) UserInfo Fetchcompletionhandler: (void (^) (Uibackgroundfetchresult)) Completionhandler { }

But the callback at the front desk.

-(void) Usernotificationcenter: (unusernotificationcenter *) Center willpresentnotification: ( Unnotification *) Notification Withcompletionhandler: (void (^) (unnotificationpresentationoptions)) Completionhandler

callback when entering from the background

-(void) Usernotificationcenter: (unusernotificationcenter *) Center Didreceivenotificationresponse: ( Unnotificationresponse *) Response Withcompletionhandler: (void (^) ()) Completionhandler

Just say how to use it:

1, Import header file

#import <UserNotifications/UserNotifications.h>

2, Registration notice

#define IOS8 ([[Uidevice currentdevice].systemversion Doublevalue] >= 8.0 && [[Uidevice Currentdevice]. Systemversion Doublevalue] < 9.0)#define ios8_10 ([[[[Uidevice Currentdevice] systemversion] floatvalue] >= 8.0 && [[Uidevice currentdevice].systemversion Doublevalue] < 10.0)#define IOS10 ([[[Uidevice] Currentdevice] systemversion] floatvalue] >= 10.0)

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) LaunchOptions method

If(IOS10) {Unusernotificationcenter *center =[Unusernotificationcenter Currentnotificationcenter]; Center.Delegate =Self [Center requestauthorizationwithoptions: (Unauthorizationoptionbadge | Unauthorizationoptionsound | Unauthorizationoptionalert) completionhandler:^ (BOOL Granted, Nserror *_nullable error) {if (!error) {NSLog (@ "succeeded!") ); } }]; } Else if (ios8_10) {//ios8-ios10 uiusernotificationsettings *settings = [uiusernotificationsettings Settingsfortypes: (Uiusernotificationtypebadge | Uiusernotificationtypealert | uiusernotificationtypesound) Categories:nil]; [Application registerusernotificationsettings:settings]; [Application registerforremotenotifications]; } Else {//IOS8 below [application Registerforremotenotificationtypes:uiremotenotificationtypebadge | Uiremotenotificationtypealert | Uiremotenotificationtypesound]; }

3, callback method, get notification data (the foreground is similar to do not do description)

-(void) Usernotificationcenter: (unusernotificationcenter *) Center Didreceivenotificationresponse: ( Unnotificationresponse *) Response Withcompletionhandler: (void (^) ()) Completionhandler {    *   UserInfo = Response.notification.request.content.userInfo; Message processing}


4, no changes to local notifications will still be callback

-(void) Application: (UIApplication *) application didreceivelocalnotification: (Uilocalnotification *) Notification

IOS10 Push Notification adaptation

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.