This article reprinted to http://blog.csdn.net/pjk1129/article/details/39551887
Original Sticker Address: https://parse.com/tutorials/ios-push-notifications
GitHub Address: Https://github.com/ParsePlatform/PushTutorial
IOS push notification has been widely used in real-world development, IOS8 and previously registered push is different, here is how to sneak into the code to record, for details, please see the above address link
Adding Code for a Push Enabled IOS application
We are now ready to start programming. We need to make a few modification to the app delegate on order to receive push notifications.
To register the current device for push, call the method[application Registerforremotenotifications] in the app de Legate ' s-application:didfinishlaunchingwithoptions: method.
-(BOOL) Application: (uiapplication *) applicationdidfinishlaunchingwithoptions:( Nsdictionary *) launchoptions { ... //Register for Push notitications, if running IOS 8 if ([Applicationrespondstoselector:@selector(registerusernotificationsettings:)]) { 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:(Uiremotenotificationtypebadge | Uiremotenotificationtypealert | Uiremotenotificationtypesound)]; } ... } |
If the registration is successful, the callback method-application: Didregisterforremotenotificationswithdevicetoken: In the application delegate'll be executed. We'll need to implement this method and use it to inform the 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 = [pfinstallationcurrentinstallation]; [currentinstallation setdevicetokenfromdata:d Evicetoken]; currentinstallation. Channels = @[@ "global" ]; [currentinstallation saveinbackground]; } |
When a push notification was received while the application was not in the foreground, it was displayed in the IOS Notificati On Center. However, if the notification is received while the app is active, it's up to the app to handle it. 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 would create a modal alert and display the push notification ' s content.
-(void) Application: (uiapplication *) applicationdidreceiveremotenotification:( Nsdictionary *) userInfo { [Pfpush handlepush: userInfo]; } |
You should now run your application (on your IOS device) to make sure everything are set up correctly. If It is, the first time you run this app you should see a modal alert requesting permission from the user to send push no Tifications.
IOS8 Push Notifications