給iOS程式添加push代碼

來源:互聯網
上載者:User
給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
github地址:https://github.com/ParsePlatform/PushTutorial

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.