Add push code to iOS programsAdding Code for a Push Enabled IOS application
Now that we've started developing the project, we need to make some changes to the program in order for the app to accept push notifications.
We are now ready to start programming. We need to make a few modification to the app delegate on order to receive push notifications.
1. Register a push notification for the current device in the app Delegete-application:didfinishlaunchingwithoptions: Call method: [Application Registerforremotenotifications]
to register the current device for push, call the Method[application Registerforremotenotifications] in the app Delega Te ' S-application:didfinishlaunchingwithoptions:method.
/* Code */-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions {...//Register for Push notitications, if running IOS 8 if ([Application respondstoselector: @selector (Registeruserno Tificationsettings:)]) {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: (Uiremoteno Tificationtypebadge | Uiremotenotificationtypealert | Uiremotenotificationtypesound)]; } ...}
2. If the registration push for the previous step succeeds, then the callback function Method-application:didregisterforremotenotificationswithdevicetoken
Called in the appdelegate. We need to implement this method and use it to notify the parsing of this new device.
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 = [pfinstallation currentinstallation]; [Currentinstallation Setdevicetokenfromdata:devicetoken]; Currentinstallation.channels = @[@ "global"]; [Currentinstallation Saveinbackground];}
3. When a push notification comes in, the program is not in the foreground and the notification is displayed in the iOS system Notification Center, however, the app is active if you receive a push notification
In order to achieve this, the notification can be played on the app, we can implement: Delegate method of the app [Application:didreceiveremotenotification]
In this case, we simply call parse, the parser creates a modal alert and displays the contents of the push.
Also: If the program exits completely, first call Didfinishlaunchingwithoptions to start the program.
Span class= "S1" style= "font-family:arial; font-size:14px; LINE-HEIGHT:26PX ">when A push notification is received while the application are not in the foreground, it's displayed in The IOS Notification Center. However, if the notification is received while the app is active, it's up to the app to handle it. To does 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 would 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 above is set correctly. If correct, you should see a modal alert when you first launch the app to ask the user
Whether to allow push notifications to be sent.
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.
Original Sticker Address: https://parse.com/tutorials/ios-push-notifications
GitHub Address: Https://github.com/ParsePlatform/PushTutorial
Add push code to iOS programs