iOS8 Push Notifications
来源:互联网 发布:learnx mac 编辑:程序博客网 时间:2024/05/20 20:05
原贴地址:https://parse.com/tutorials/ios-push-notifications
github地址:https://github.com/ParsePlatform/PushTutorial
iOS Push通知已经广泛应用于实际开发中,iOS8与之前注册push有所不同,这里把如何潜入代码贴一下,以作记录,详情请看上面地址链接
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 in order to receive push notifications.
To register the current device for push, call the method[application registerForRemoteNotifications] in the app delegate'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 will be executed. We will need to implement this method and use it to inform Parse about this new device.
- (void)application:(UIApplication *)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallationcurrentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
currentInstallation.channels = @[@"global" ];
[currentInstallation saveInBackground];
}
When a push notification is received while the application is not in the foreground, it is displayed in the iOS Notification Center. However, if the notification is received while the app is active, it is up to the app to handle it. To do so, we can implement the [application:didReceiveRemoteNotification] method in the app delegate. In our case, we will simply ask Parse to handle it for us. Parse will 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 is 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 notifications.
- iOS8 Push Notifications
- iOS8 Push Notifications
- Local Notifications and Push Notifications
- WP7的Push Notifications
- iOS Push Notifications | Parse
- Implement Push Notifications for Android
- Microsoft Push Notifications初体验
- iOS8 PUSH解决方法
- iOS8 PUSH解决方法
- iOS8 PUSH解决方法
- iOS8 PUSH解决方法
- iOS8 PUSH解决方法
- How to Implement Push Notifications for Android
- How to Implement Push Notifications for Android
- IOS PUSH NOTIFICATIONS USING JABBER AND EJABBERD
- How to Implement Push Notifications for Android
- Erlang实现Apple Push Notifications消息推送
- iOS Push Notifications第二项老是红色
- C#时间/日期格式大全
- hibernate 延迟加载
- 一个jq投票,无论成功与否3秒后自动关闭提示框
- iOS七牛云存储
- arcgis基础(2)
- iOS8 Push Notifications
- 单链表操作(数据结构实验一)
- 《敏捷武士:看敏捷高手交付卓越软件》读书摘录
- java对象拷贝
- muleStudio与Maven集成
- Hibernate高级查询方法
- Cocos2dx-3.2编译错误error C1083: 无法打开包括文件: “extensions/ExtensionMacros.h”: No such file or directory
- Android中ZXing简单使用
- Log4j配置很详细