簡體   English   中英

如何處理推送通知(取決於iOS版本)?

[英]How to handle push notifications depending on iOS version?

對於iOS7,Parse使用AppDelegate中的以下代碼處理了推送通知:

[application registerForRemoteNotificationTypes:
 UIRemoteNotificationTypeBadge|
 UIRemoteNotificationTypeAlert|
 UIRemoteNotificationTypeSound];

registerForRemoteNotificationTypes不iOS8上支持,但和用於處理推iOS8上的通知,現在看起來像這樣新的代碼:

UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |
 UIUserNotificationTypeBadge |
 UIUserNotificationTypeSound
                                  categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];

在iOS7設備上使用此新代碼會導致應用程序崩潰,因此我需要讓代碼確定手機所使用的版本,並運行適當的推送通知代碼。 我如何讓該應用檢查此內容並使用正確的應用?

檢查方法的可用性總是比檢查OS版本更好。

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)]) {

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    [[UIApplication sharedApplication] registerForRemoteNotifications];

} else {

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}

假設您的部署目標是> = 7.0。

可能與iOS 8.0及更高版本中registerForRemoteNotificationTypes:重復

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM