使用Info.plist中配置的UIBackgroundModes检测推送通知

时间:2016-03-31 16:02:34

标签: ios objective-c notifications apple-push-notifications

这是我的问题:

我为检测到的用户启用了或不启用推送通知

实现了此代码
if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {
  NSLog(@"Yes, s(he) accepted Push Notification");
} else {
  NSLog(@"No, s(he) rejected Push Notification");
}

哪个,效果很好。但我必须实现在后台接收静默推送通知,所以我在Info.plist

中添加了此代码
<key>UIBackgroundModes</key>
<array>
  <string>remote-notification</string>
</array>

然后,当我添加此内容时,检测到用户是否启用Push notification的代码始终返回TRUE,无论用户是否启用推送通知都无关紧要。有没有办法只检测用户是否启用了可见的推送通知(手机顶部的警报),允许UIBackgroundModes?

1 个答案:

答案 0 :(得分:0)

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]是否正确使用。要了解用户关于推送通知的决定,请参阅 的 currentUserNotificationSettings

UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];

if (!notificationSettings || (notificationSettings.types == UIUserNotificationTypeNone)) {
      isEnabled = NO;
} else {
      isEnabled = YES;
}