Implementation of IOS push

Source: Internet
Author: User
<span id="Label3"></p><p><p>-(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions {</p></p><p><p></p></p><p><p>Self.window = [[uiwindow Alloc]initwithframe:[uiscreen mainscreen].bounds];</p></p><p><p></p></p><p><p></p></p><p><p>Handling Remote Push</p></p><p><p>If (launchoptions && [launchoptions valueforkey:uiapplicationlaunchoptionsremotenotificationkey]) {</p></p><p><p>[self checkremotenotification:[launchoptions valueforkey:uiapplicationlaunchoptionsremotenotificationkey]];</p></p><p><p>}</p></p><p><p></p></p><p><p>Background (not activated) execution when receiving server messages</p></p><p><p>If ([uiapplication sharedapplication].applicationiconbadgenumber! = 0) {</p></p><p><p>Requirements when adding to a push message</p></p><p><p>}</p></p><p><p>The Add-in app will remove the notification prompts</p></p><p><p>[uiapplication Sharedapplication].applicationiconbadgenumber = 0;</p></p><p><p></p></p><p><p>Turn on remote push</p></p><p><p>[self openremotenotification];</p></p><p><p></p></p><p><p>Return YES;</p></p><p><p>}</p></p><p><p></p></p><p><p>Turn on push</p></p><p><p>-(void) Openremotenotification {</p></p><p><p>Turn on push</p></p><p><p>If (!ios7) {</p></p><p><p></p></p><p><p>[[uiapplication sharedapplication] registerforremotenotifications];</p></p><p><p>uiusernotificationsettings* requestedsettings = [uiusernotificationsettings settingsfortypes: ( Uiusernotificationtypealert | Uiusernotificationtypesound | Uiusernotificationtypebadge) categories:nil];</p></p><p><p>[[uiapplication sharedapplication] registerusernotificationsettings:requestedsettings];</p></p><p><p></p></p><p><p>} else {</p></p><p><p></p></p><p><p>Enable APNS</p></p><p><p>[[uiapplication sharedapplication]</p></p><p><p>Registerforremotenotificationtypes: (uiremotenotificationtypealert | Uiremotenotificationtypebadge | uiremotenotificationtypesound)];</p></p><p><p></p></p><p><p>}</p></p><p><p></p></p><p><p>}</p></p><p><p></p></p><p><p>#pragma Mark-anps</p></p><p><p>Registration successful</p></p><p><p>-(void) application: (uiapplication *) application didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken {</p></p><p><p></p></p><p><p>If (devicetoken) {</p></p><p><p>NSString * devicetokenstring = [[[devicetoken description] stringbytrimmingcharactersinset:[nscharacterset charactersetwithcharactersinstring:@ "<>"] stringbyreplacingoccurrencesofstring:@ "" withstring:@ ""];</p></p><p><p>.............</p></p><p><p>The devicetokenstring is passed to the server here</p></p><p><p></p></p><p><p>}</p></p><p><p>}</p></p><p><p></p></p><p><p>Receive a remote push</p></p><p><p>-(void) application: (uiapplication *) application didreceiveremotenotification: (nsdictionary *) userInfo {</p></p><p><p></p></p><p><p>Remote push processing</p></p><p><p>[self checkremotenotification:userinfo];</p></p><p><p></p></p><p><p>}</p></p><p><p>Failed to enroll device</p></p><p><p>-(void) application: (uiapplication*) application didfailtoregisterforremotenotificationswitherror: (NSError*) Error</p></p><p><p>{</p></p><p><p>NSLog (@ "Failed to get token, error:%@", error);</p></p><p><p>}</p></p><p><p>Click the app icon in the background to enter</p></p><p><p>-(void) applicationwillenterforeground: (uiapplication *) Application {</p></p><p><p>Determine if you have received a message notification</p></p><p><p>If ([uiapplication sharedapplication].applicationiconbadgenumber! = 0) {</p></p><p><p>Requirements when adding to a push message</p></p><p><p>}</p></p><p><p></p></p><p><p>[uiapplication Sharedapplication].applicationiconbadgenumber = 0;</p></p><p><p>}</p></p><p><p>Remote push processing</p></p><p><p>-(void) checkremotenotification: (nsdictionary*) UserInfo {</p></p><p><p></p></p><p><p>if (userInfo = = Nil | | [userInfo Iskindofclass:[nsnull class]]) {</p></p><p><p>Return</p></p><p><p>}</p></p><p><p>#if! Target_iphone_simulator</p></p><p><p></p></p><p><p><span class="Apple-tab-span">NSLog (@ "remote notification:%@", [userInfo description]);</span></p></p><p><p></p></p><p><p>Nsdictionary *apsinfo = [userInfo valueforkey:@ "aps"];</p></p><p><p>NSString *alert = [apsinfo valueforkey:@ "alert"];</p></p><p><p>NSNumber *badge = [apsinfo valueforkey:@ "badge"]; This body must have</p></p><p><p></p></p><p><p>NSNumber *type = [userInfo valueforkey:@ "type"];</p></p><p><p>NSNumber *menu = [userInfo valueforkey:@ "menu"];</p></p><p><p>NSString *text = [userInfo valueforkey:@ "text"];</p></p><p><p>NSString *link = [userInfo valueforkey:@ "link"];</p></p><p><p>NSString *val = [userInfo valueforkey:@ "val"];</p></p><p><p></p></p><p><p>If ((!menu | | [menu Iskindofclass:[nsnull class]]) ||</p></p><p><p>(!type | | [type Iskindofclass:[nsnull class]]) ){</p></p><p><p>Return</p></p><p><p>}</p></p><p><p></p></p><p><p>Display of the APP icon message prompt</p></p><p><p>If ([apsinfo valueforkey:@ "badge"] &&![ [apsinfo valueforkey:@ "badge"] iskindofclass:[nsnull class]]) {</p></p><p><p>If ([self cansendnotifications]) {</p></p><p><p>[uiapplication sharedapplication].applicationiconbadgenumber = [[apsinfo valueforkey:@ "badge"] integerValue];</p></p><p><p>}</p></p><p><p>}</p></p><p><p></p></p><p><p>The server shouted I opened the app and went to Work.</p></p><p><p>If ([type integervalue] = = 3) {</p></p><p><p></p></p><p><p>Nsmutabledictionary *apnsinfo = [nsmutabledictionary dictionarywithdictionary:userinfo];</p></p><p><p>[apnsinfo setobject:@ ([uiapplication sharedapplication].applicationstate = = Uiapplicationstateactive) forKey:@ " IsActive "];</p></p><p><p></p></p><p><p>Push Notifications</p></p><p><p>[[nsnotificationcenter defaultcenter] Postnotificationname:k_noti_apns_push object:apnsinfo];</p></p><p><p></p></p><p><p>}</p></p><p><p></p></p><p><p>#endif</p></p><p><p></p></p><p><p>}</p></p><p><p></p></p><p><p>Determine if you can receive push</p></p><p><p>-(BOOL) cansendnotifications;</p></p><p><p>{</p></p><p><p>If (iOS7) {</p></p><p><p>Return YES;</p></p><p><p>}</p></p><p><p></p></p><p><p>uiusernotificationsettings* notificationsettings = [[uiapplication sharedapplication] currentusernotificationsettings];</p></p><p><p>BOOL cansendnotifications = NO;</p></p><p><p>if (notificationsettings.types = = (uiusernotificationtypebadge | Uiusernotificationtypesound | Uiusernotificationtypealert)) {</p></p><p><p>Cansendnotifications =yes;</p></p><p><p>}</p></p><p><p>Return cansendnotifications;</p></p><p><p>}</p></p><p><p></p></p><p><p>Implementation of IOS push</p></p></span>

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.