Add a push code to the IOS program

Source: Internet
Author: User
Tags notification center
Add a push code to the iOS program adding the code for a push Enabled iOS application

Now we're starting to develop the project, and in order for the app to accept push notifications, we need to make some changes to the program.
We are now ready to start programming. We need to make a few modification to the app delegate in order to receive push notifications.

1. Register push notice to current device,-application:didfinishlaunchingwithoptions in app Delegete: Call method: [Application Registerforremotenotifications]
To register the "device for push", call the "Method[application Registerforremotenotifications] in the app delegate ' s -application:didfinishlaunchingwithoptions:method.
/* Code/* (BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions {...//Register for Push notitications, if running IOS 8 if ([Application respondstoselector: @selector (Registerus

                                                    Ernotificationsettings:)] {Uiusernotificationtype usernotificationtypes = (Uiusernotificationtypealert |

                                                    Uiusernotificationtypebadge |

    Uiusernotificationtypesound);

                                                                            Uiusernotificationsettings *settings = [uiusernotificationsettingssettingsfortypes:usernotificationtypes

    Categories:nil];

    [Application registerusernotificationsettings:settings];

  [Application registerforremotenotifications]; else {//Register for Push notifications before IOS 8 [application registerforremotenotificationtypes: (Uiremo

               Tenotificationtypebadge |                                      Uiremotenotificationtypealert |

  Uiremotenotificationtypesound)]; }
  ...
}

2. If the registration push succeeds in the previous step, then the callback function Method-application:didregisterforremotenotificationswithdevicetoken
Called in Appdelegate. We need to implement this method and use it to inform the resolution of this new device.
If the registration is successful, the callback Method-application:didregisterforremotenotificationswithdevicetoken: In the application delegate would be executed. We'll need to implement this and use it to inform Parse about this new device.
-(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken {//Store the Devicetoken in the ' current installation ' and


  save it to Parse.


  Pfinstallation *currentinstallation = [pfinstallation currentinstallation];


  [Currentinstallation Setdevicetokenfromdata:devicetoken];


  Currentinstallation.channels = @[@ "global"];


  [Currentinstallation Saveinbackground];
}

3. When a push notification comes over, the program is not in the foreground, the notice is displayed in the iOS system Notification Center, however, if the push notification is received, the app is active
In order to achieve this, the notice can be played on the app, we can implement: app's Delegate method [Application:didreceiveremotenotification]

In this case, we simply call the parse, and the parser creates a modal alert and displays the contents of the push.

In addition: If the program exits completely, first call Didfinishlaunchingwithoptions to start the program.

When a push notification are received while the application is not in the foreground, it is displayed in the IOS Notificati On Center. However, if the notification is received while the "app is" active, it is up to the app to handle it. To did so, we can implement the [Application:didreceiveremotenotification] method in the app delegate. In our case, we'll simply ask Parse to handle it for us. Parse'll create a modal alert and display the push notification ' s content.

-(void) Application: (UIApplication *) application didreceiveremotenotification: (Nsdictionary *) userInfo {

  [ Pfpush handlepush:userinfo];

}

4. Now, you can start the app to check if the logic is set correctly. If it is correct, you should see a modal alert to ask the user for the first time you start the app

Whether push notifications are allowed to be sent.

You are should now run your application [on your IOS device] to make sure everything are set up correctly. If It is, the the "the" the "I" you run this app for you should-modal alert requesting permission from the user to send push no Tifications.

Original posted Address: https://parse.com/tutorials/ios-push-notifications
GitHub Address: Https://github.com/ParsePlatform/PushTutorial

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.