6

iOS 8 中的推送通知不起作用。

错误显示:

implicit conversion of 'unsigned long 'UIUserNotificationSettings *' is disallowed with arc

代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [application registerUserNotificationSettings:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)];
    return YES;
}

在此处输入图像描述 我正在使用 ios 8.0 和 xcode 6 beta。

4

5 回答 5

7

我是从iOS 8 的官方文档中得到的。

  • 使用本地或推送通知的应用程序必须使用 UIUserNotificationSettings 对象显式注册它们向用户显示的警报类型。此注册过程与注册远程通知的过程是分开的,用户必须授予通过请求的选项传递通知的权限。
  • 本地和推送通知可以包括自定义操作作为警报的一部分。自定义操作在警报中显示为按钮。点击后,您的应用会收到通知并要求您执行相应的操作。本地通知也可以通过与核心位置区域的交互来触发。

并且还阅读

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIUserNotificationSettings_class/index.html#//apple_ref/occ/cl/UIUserNotificationSettings

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instm/UIApplication/registerUserNotificationSettings

所以答案应该是..

/// First register notification setting with settings type like 
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications]; // you can also set here for local notification.
于 2014-06-03T04:53:22.300 回答
5
- (void)registerForRemoteNotificationTypes:(NSUInteger)notificationTypes categories:(NSSet *)categories
{
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:notificationTypes categories:categories]];
    }
    else if ([UIApplication instancesRespondToSelector:@selector(registerForRemoteNotificationTypes:)])
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
    }
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    if ([UIApplication instancesRespondToSelector:@selector(registerForRemoteNotifications)])
    {
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
}

试试UIUserNotificationSettings-Extension,它提供了帮助方法,让你更容易处理新的#iOS8 #Interactive #Notifications。

于 2014-06-04T21:05:03.310 回答
1

请查看运行时提供的日志。起初,没有用户注册本地事件,日志确实建议

UILocalNotificationInfiniteRepeatCount, next fire date = Wednesday, 4 June 2014 9:27:24 pm India Standard Time, user info = (null)} with an alert but haven't received permission from the user to display alerts.

这是iOS 8。

因此,在这种情况下,您还需要使用

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil]];

didFinishLaunchingWithOptions.

于 2014-06-04T16:08:09.260 回答
1

这里是:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
// are you running on iOS8?

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) 
  {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
    [application registerUserNotificationSettings:settings];
  } 
else // iOS 7 or earlier
  {
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [application registerForRemoteNotificationTypes:myTypes];
  }
}
于 2014-11-19T06:12:48.180 回答
0

这就是你需要处理 iOS 8 和低于 iOS 8 的任何东西

if (SYSTEM_VERSION_LESS_THAN(@"8.0")) {
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert];
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeNone];
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge];
} else {
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
于 2014-10-02T00:55:39.210 回答