iOS8 Push Notifications

本文转载至 http://blog.csdn.net/pjk1129/article/details/39551887

原贴地址: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.

时间: 2024-11-09 17:43:18

iOS8 Push Notifications的相关文章

使用Google Cloud Messaging (GCM),PHP 开发Android Push Notifications (安卓推送通知)

什么是GCM? Google Cloud  Messaging (GCM) 是Google提供的一个服务,用来从服务端向安卓设备发送推送通知. GCM分为客户端和服务端开发. 这里我们只介绍服务端开发.其实过程非常简单,只需利用PHP发送POST数据. api key的取得? 待补充? class GCM { public $api_key = "AIzaSyAU3wZs9raik-mHQ"; function __construct() { } /** * Sending Push

Send Push Notifications to iOS Devices using Xcode 8 and Swift 3, APNs Auth Key

Send Push Notifications to iOS Devices using Xcode 8 and Swift 3 OCT 6, 2016 Push notifications are a great way to ensure your users re-engage with your app every once in a while, but implementing them on iOS can be challenging, especially with all o

iOS10 App适配权限 Push Notifications 字体Frame 遇到的坑!!!!

添加配置权限 <!-- 相册 --> <key>NSPhotoLibraryUsageDescription</key> <string>"xx"想使用您的相册,需要您的允许</string> <!-- 相机 --> <key>NSCameraUsageDescription</key> <string>"xx"想使用您的相机,需要您的允许</strin

[PWA] Add Push Notifications to a PWA with React in Chrome and on Android

On Android and in Chrome (but not on iOS), it's possible to send push notifications with a PWA. We'll start by asking the user for permission to send them push notifications, and then look at how to intercept the push event in a service worker. We ca

iOS8 PUSH解决方法

本文转载至 http://blog.csdn.net/pjk1129/article/details/39548523 - (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types NS_DEPRECATED_IOS(3_0, 8_0, "Please use registerForRemoteNotifications and registerUserNotificationSettings: instea

IOS8 PUSH

registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later // IOS8 新系统需要使用新的代码咯if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificatio

iOS中远程推送实现—在Apple的生产环境上测试Push Notifications功能

1.在“Provisioning Profiles”中点击“Add”按钮. 2.在“What type of provisioning profile do you need?”页面中选择“Distribution”下的“Ad Hoc”,点击“Continue”按钮,如下图 3.在“Select App ID.”页面,选择“App ID”,点击“Continue”按钮. 4.在“Select certificates.”页面中,选中证书,点击“Continue”按钮. 5.在“Select de

Android Push Notifications using Google Cloud Messaging (GCM), PHP and MySQL

http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ 按照文章的链接的步骤,实现GCM-HTTP在client 和app server之间的部署.链接一下供须要的同学參考. 在从本地Server移植到web server遇到的问题: 1.Google Api Key是绑定serverIP的,所以在本地换到Web的时候,Google

IOS8下的远程推送(转载)

原文地址:http://blog.sina.com.cn/s/blog_71715bf80101615c.html 昨天做了一下远程推送,今天写下来,分享给需要的人.参考了很多篇文章,或许是iOS8的改动,没有一篇可以完整的看下来,所以打算自己写一篇. 后台我也写了,用的是SAE,PHP代码,很简单,调用SAE封装好的一个类就可以向APNS发推送信息. 首先,来说一下苹果的推送机制.顾名思义,推送,是指服务器向客户端发送消息,那么在iOS中,应用是被后台挂起的,并不能一直连接网络,那么服务器怎么