IOS10 New push function implementation code _ios

Source: Internet
Author: User

Starting from the iOS8.0 the implementation of the push function is constantly changing, the function is also increasing, iOS10 again out of a push plug-in development (see the final picture), nonsense not to say more directly on the code:  

#import <UserNotifications/UserNotifications.h>
 

-(BOOL) Application: (UIApplication *) application Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions {
 //Override point for customization after Application launch.
 
 * App does not start, click Push Message in case iOS10 abandoned Uiapplicationlaunchoptionslocalnotificationkey, Using the proxy Unusernotificationcenterdelegate method Didreceivenotificationresponse:withcompletionhandler: Get the local push
 *
/ /nsdictionary *localuserinfo = Launchoptions[uiapplicationlaunchoptionslocalnotificationkey];
if (localuserinfo) {
//NSLog (@ "localuserinfo:%@", localuserinfo);
App not started, click Push message
//}
 nsdictionary *remoteuserinfo = launchoptions[ Uiapplicationlaunchoptionsremotenotificationkey];
 if (remoteuserinfo) {
 NSLog (@ "remoteuserinfo:%@", remoteuserinfo);
 App does not start, click Push Message, iOS10 or as before get here
 }
 [self registernotification];
 Return YES
}

To register changes to the Push method:

New library #import <UserNotifications/UserNotifications.h>  push single row unusernotificationcenter, etc. api 

-(void) registernotification{/* Identifier: The behavior identifier that is used to identify the behavior when the proxy method is invoked.
 Title: Behavior name.
 Uiusernotificationactivationmode: That is, whether the behavior opens the app.
 Authenticationrequired: Do you need to unlock it?
 Destructive: This decision button shows the color, yes the button will be red. Behavior: Click on the button text input, whether eject keyboard * * * unnotificationaction *action1 = [unnotificationaction actionwithidentifier:@ "Action1"
 title:@ "Strategy 1 Act 1" options:unnotificationactionoptionforeground];
 /*ios9 Implementation Method Uimutableusernotificationaction * Action1 = [[Uimutableusernotificationaction alloc] init];
 Action1.identifier = @ "Action1";
 action1.title=@ "Strategy 1 Act 1";
 Action1.activationmode = Uiusernotificationactivationmodeforeground;
 Action1.destructive = YES; * * untextinputnotificationaction *action2 = [untextinputnotificationaction actionwithidentifier:@ action2 "title:@" Strategy 1 Behavior 2 "options:unnotificationactionoptiondestructive textinputbuttontitle:@" Textinputbuttontitle "
 textinputplaceholder:@ "Textinputplaceholder"]; /*ios9 Implementation Method Uimutableusernotificationaction * Action2 = [[Uimutableusernotificationaction alLOC] init];
 Action2.identifier = @ "Action2";
 action2.title=@ "Strategy 1 act 2";
 Action2.activationmode = Uiusernotificationactivationmodebackground;
 action2.authenticationrequired = NO;
 Action2.destructive = NO; Action2.behavior = uiusernotificationactionbehaviortextinput;//Click on the button text input, whether to eject the keyboard * * Unnotificationcategory *category1 = [Unnotificationcategory categorywithidentifier:@ "Category1" Actions:@[action2,action1] minimalactions:@[action2,
 Action1] intentidentifiers:@[@ "Action1", @ "Action2"] options:unnotificationcategoryoptioncustomdismissaction];
 Uimutableusernotificationcategory * Category1 = [[Uimutableusernotificationcategory alloc] init];
 Category1.identifier = @ "Category1";
 
 [Category1 Setactions:@[action2,action1] Forcontext: (Uiusernotificationactioncontextdefault)]; Unnotificationaction *action3 = [unnotificationaction actionwithidentifier:@ "Action3" title:@ "policy 2 behavior 1" options:
 Unnotificationactionoptionforeground]; Uimutableusernotificationaction * Action3 = [[UimuTableusernotificationaction alloc] init];
 Action3.identifier = @ "Action3";
 action3.title=@ "Strategy 2 Act 1";
 Action3.activationmode = Uiusernotificationactivationmodeforeground;
 
 Action3.destructive = YES; Unnotificationaction *action4 = [unnotificationaction actionwithidentifier:@ "Action4" title:@ "policy 2 Behavior 2" options:
 Unnotificationactionoptionforeground];
 Uimutableusernotificationaction * Action4 = [[Uimutableusernotificationaction alloc] init];
 Action4.identifier = @ "Action4";
 action4.title=@ "Strategy 2 Act 2";
 Action4.activationmode = Uiusernotificationactivationmodebackground;
 action4.authenticationrequired = NO;
 
 Action4.destructive = NO; Unnotificationcategory *category2 = [unnotificationcategory categorywithidentifier:@ "Category2" Actions:@[action3, Action4] Minimalactions:@[action3,action4] intentidentifiers:@[@ "Action3", @ "Action4"] options:
 Unnotificationcategoryoptioncustomdismissaction]; Uimutableusernotificationcategory * Category2 = [[UimutableusernoTificationcategory alloc] init];
 Category2.identifier = @ "Category2";
 
 
 [Category2 Setactions:@[action4,action3] Forcontext: (Uiusernotificationactioncontextdefault)]; [[Unusernotificationcenter Currentnotificationcenter] Setnotificationcategories:[nsset setWithObjects:category1,
 Category2, Nil]]; [[Unusernotificationcenter Currentnotificationcenter] Requestauthorizationwithoptions:unauthorizationoptionbadge | Unauthorizationoptionsound |
 Unauthorizationoptionalert completionhandler:^ (BOOL Granted, Nserror * _nullable error) {NSLog (@ "Completionhandler");
 }]; /*ios9 Implementation method Uiusernotificationsettings *settings = [uiusernotificationsettings settingsfortypes: ( Uiusernotificationtypealert| uiusernotificationtypebadge|

 Uiusernotificationtypesound) Categories:[nsset Setwithobjects:category1,category2, Nil]];
 [[UIApplication sharedapplication] registerusernotificationsettings:settings];
 
 
 * [[UIApplication sharedapplication] registerforremotenotifications]; [UnusernotificatioNcenter currentnotificationcenter].delegate = self;

 }

Change of Agent Method:

Some local and remote push callbacks are placed in the same proxy method

#pragma mark-(void) Application: (UIApplication *) application didregisterusernotificationsettings: ( Uiusernotificationsettings *) notificationsettings Ns_available_ios (8_0) __tvos_prohibited{NSLog (@ "
Didregisterusernotificationsettings "); }-(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *)
 Devicetoken Ns_available_ios (3_0) {NSLog (@ "devicetoken:%@", Devicetoken); NSString *devicetokenst = [[[Devicetoken description] stringbyreplacingoccurrencesofstring:@ ' < ' withstring:@ '] s tringbyreplacingoccurrencesofstring:@ ">" withstring:@ ""] stringbyreplacingoccurrencesofstring:@ "" withString:@
 ""];
NSLog (@ "devicetokenst:%@", devicetokenst); }-(void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: (NSError *)

Error Ns_available_ios (3_0) {NSLog (@ "didfailtoregisterforremotenotificationswitherror:%@", error);} /*ios9 use Method-(void) Application: (UIApplication *) Application DidreceivereMotenotification: (nsdictionary *) UserInfo Ns_deprecated_ios (3_0, 10_0, "Use Usernotifications Framework '-[ Unusernotificationcenterdelegate Willpresentnotification:withcompletionhandler:] or-[ Unusernotificationcenterdelegate Didreceivenotificationresponse:withcompletionhandler:] for user visible Notifications and-[uiapplicationdelegate Application:didReceiveRemoteNotification:fetchCompletionHandler:] for Silent Remote Notifications ") {} */-(void) Usernotificationcenter: (unusernotificationcenter *) Center Willpresentnotif Ication: (unnotification *) notification Withcompletionhandler: (void (^) (unnotificationpresentationoptions))
 
 completionhandler{NSLog (@ "willpresentnotification:%@", notification.request.content.title);
 
Here the real need to handle the interaction of the place//get the data with the notification nsstring *notmess = [Notification.request.content.userInfo objectforkey:@ "APS"]; }-(void) Usernotificationcenter: (unusernotificationcenter *) Center Didreceivenotificationresponse: ( Unnotificationresponse *) Response WithcompletionhaNdler: (void (^)) completionhandler{//When the app is not started, receive the server push message, the DROP-DOWN message will have a Quick response button, click the button after the call method, according to the identifier to determine which button clicked
 NSString *notmess = [Response.notification.request.content.userInfo objectforkey:@ "APS"];
NSLog (@ "didreceivenotificationresponse:%@", response.notification.request.content.title); Response.notification.request.identifier}//remote push app at foreground-(void) Application: (UIApplication *) application Didreceiveremotenotification: (nsdictionary *) userInfo Fetchcompletionhandler: (void (^) (Uibackgroundfetchresult))

completionhandler{NSLog (@ "didreceiveremotenotification:%@", UserInfo);} /*-(void) Application: (UIApplication *) application Handleactionwithidentifier: (Nullable NSString *) identifier Forremotenotification: (nsdictionary *) userInfo Completionhandler: (void (^) ()) Completionhandler Ns_deprecated_ios (8 _0, 10_0, "use usernotifications Framework ' s-[unusernotificationcenterdelegate didreceivenotificationresponse: Withcompletionhandler:] "__tvos_prohibited {} *////////local notification callback function, when application is called at foreground
-(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification *) Notification NS _deprecated_ios (4_0, 10_0, "Use Usernotifications Framework '-[unusernotificationcenterdelegate Willpresentnotification:withcompletionhandler:] or-[unusernotificationcenterdelegate Didreceivenotificationresponse:withcompletionhandler:] "__tvos_prohibited{NSLog (@) Didreceivelocalnotification:
 
 
 %@ ", notification.userinfo);
 Here the real need to handle the interaction of the place//get the data with the notification nsstring *notmess = [Notification.userinfo objectforkey:@ "APS"]; Uialertview *alert = [[Uialertview alloc] initwithtitle:@ ' local notification (foreground) ' Message:notmess Delegate:nil
 tle:@ "OK" otherbuttontitles:nil];
 
 [Alert show];
 Update the number of badges displayed Nsinteger badge = [UIApplication sharedapplication].applicationiconbadgenumber;
 badge--; Badge = Badge >= 0?
 badge:0;
 
 [UIApplication sharedapplication].applicationiconbadgenumber = badge; When you do not need to push again, you can cancel the push [Firstviewcontroller CANCELLOCALNOTIFICATIONWIthkey:@ "Key"]; }-(void) Application: (UIApplication *) application Handleactionwithidentifier: (Nullable NSString *) identifier Forlocalnotification: (uilocalnotification *) notification Completionhandler: (void (^) ()) Completionhandler Ns_ Deprecated_ios (8_0, 10_0, "Use Usernotifications Framework '-[unusernotificationcenterdelegate Didreceivenotificationresponse:withcompletionhandler:] __tvos_prohibited {//When you receive a local message when you are not in the app interface, the Drop-down message will have a quick response button.
 Click the button to call the method, according to identifier to determine which button clicked, Notification for message content NSLog (@ "%@----%@", identifier,notification);

 Completionhandler ()///Finish processing the message, finally make sure to call this code block} * *

and push plugin development: Similar to iOS Tody widget plugin development

This article has been organized into the "iOS push Tutorial", welcome to learn to read.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.