IOS10 Adaptive remote Push function implementation code _ios

Source: Internet
Author: User

IOS10 official version of the release, the online variety of adaptation XCode8 and iOS10 articles flying. But there are not many articles for the iOS10 adaptation of remote push. IOS10 for the push of the modification is very large, the new Usernotifications Framework, today on the combination of their own projects, said the actual adaptation of the situation.

First, open push notifications switch in capabilities

In the XCode7 here, the switch is not clocked, push can also be used normally, but in XCode8, the switch here must be opened, or it will be an error:

Error domain=nscocoaerrordomain code=3000 "aps-environment authorization string for application not found" userinfo={nslocalizeddescription= The application's "aps-environment" authorization string was not found}

The entitlements file is generated when you open it, where you can set up APS environment

Second, push the registration

First, we introduce the usernotifications Framework,

Import <UserNotifications/UserNotifications.h>
IOS10 modified the method of registering the push, here we have to set the different version separately. Modify the previous push settings in the application Didfinishlaunchingwithoptions method (I only implemented the settings above iOS8)

if (ios_version >= 10.0) {
  Unusernotificationcenter *center = [Unusernotificationcenter Currentnotificationcenter];
  Center.delegate = self;
  [Center requestauthorizationwithoptions: (Unauthorizationoptionbadge | Unauthorizationoptionsound | Unauthorizationoptionalert) completionhandler:^ (BOOL Granted, Nserror * _nullable error) {
   if (!error) {
    Dlog (@ "Request Authorization succeeded!");
   }
  ];
 } else {
  if ([Application respondstoselector: @selector (registerusernotificationsettings:)]) {
   //ios8, Creates a uiusernotificationsettings and sets the display class type of the message
   uiusernotificationsettings *notisettings = [ Uiusernotificationsettings settingsfortypes: (Uiusernotificationtypebadge | Uiusernotificationtypealert | Uiusernotificationtypesound) Categories:nil];
 
   [Application registerusernotificationsettings:notisettings];
  }
 

Third, unusernotificationcenterdelegate agent implementation

There are two ways to implement Unusernotificationcenterdelegate in iOS10 to handle push messages:

Copy Code code as follows:
-(void) Usernotificationcenter: (unusernotificationcenter *) Center willpresentnotification: (unnotification *) Notification Withcompletionhandler: (void (^) (unnotificationpresentationoptions)) Completionhandler
-(void) Usernotificationcenter: (unusernotificationcenter *) Center Didreceivenotificationresponse: ( Unnotificationresponse *) Response Withcompletionhandler: (void (^) ()) Completionhandler

The first is that app receives a push-and-execute callback method at the front desk, and the second is the callback method that executes when the app clicks on the push message in the background.

Before processing the push, the information is in the UserInfo parameter, and the new method shows that there is no this parameter, in fact we can get to the UserInfo, as follows:

App in the foreground callback
-(void) Usernotificationcenter: (unusernotificationcenter *) Center willpresentnotification: ( Unnotification *) notification Withcompletionhandler: (void (^) (unnotificationpresentationoptions)) Completionhandler {
 Nsdictionary *userinfo = notification.request.content.userInfo;
 [Self handleremotenotificationforcegroundwithuserinfo:userinfo];
}
 
App in the background click Push Call
-(void) Usernotificationcenter: (Unusernotificationcenter *) Center Didreceivenotificationresponse: (unnotificationresponse *) response Withcompletionhandler: (void (^) ()) Completionhandler {
 Nsdictionary *userinfo = response.notification.request.content.userInfo;
 [Self handleremotenotificationbackgroundwithuserinfo:userinfo];
}

Complete the above three steps of the setup, for the iOS10 push settings are basically fit. To customize notification content or implement other notificationaction, refer to other articles. This is just a fitting for the iOS10.

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.