IOS8 Push Notifications

Source: Internet
Author: User

 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

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.