给iOS程序添加push代码
最后更新于:2022-04-01 20:00:50
给iOS程序添加push代码 Adding Code for a Push Enabled iOS Application
现在,我们开始开发项目,为了使该App能够接受push通知,我们需要对程序进行一些修改。
We are now ready to start programming. We need to make a few modification to the app delegate in order to receive push notifications.
1.给当前设备注册push通知,在App delegete中的-application:didFinishLaunchingWithOptions: 调用方法:[application registerForRemoteNotifications]
To register the current device for push, call the method[application registerForRemoteNotifications] in the app delegate's-application:didFinishLaunchingWithOptions: method.
~~~
/*代码*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
// Register for Push Notitications, if running iOS 8
if ([application respondsToSelector:@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)];
}
...
}
~~~
2.如果上一步的注册push成功,那么回调函数method-application:didRegisterForRemoteNotificationsWithDeviceToken在
APPdelegate中被调用。我们需要实现这个方法,并用它来通知解析这种新设备。
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 *)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.当一个push通知过来的时候,程序不在前台,该通知则显示在iOS系统通知中心,然而,如果收到push通知的时候,app是active的
为了实现,该通知能在APP上面弹出来,我们可以实现:app的delegate方法[application:didReceiveRemoteNotification]
这种情况,我们简单调用解析,解析器创建一个模态alert并显示push的内容。
另外:如果程序完全退出,先调用didFinishLaunchingWithOptions把程序启动起来。
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 *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
}
~~~
4.现在,你可启动APP检查一下上面的逻辑是否都设置正确了。如果正确,第一次启动APP的时候你应该会看到一个模态alert来询问用户
是否允许发送push通知。
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.
原贴地址:[https://parse.com/tutorials/ios-push-notifications](https://parse.com/tutorials/ios-push-notifications)
github地址:[https://github.com/ParsePlatform/PushTutorial](https://github.com/ParsePlatform/PushTutorial)
';