【问题标题】:starting to work with iOS push notifications开始使用 iOS 推送通知
【发布时间】:2015-06-14 20:47:43
【问题描述】:

我开始学习我通过谷歌找到的这个教程。

http://code.tutsplus.com/tutorials/setting-up-push-notifications-on-ios--cms-21925

但是我基本上卡在了它说方法已弃用的第一步,我将它们更改为 Xcode 建议的方法 原来的代码是

   [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

我改成

  [application registerForRemoteNotification:(UIRemoteNotificationType | UIRemoteNotificationType | UIRemoteNotificationTypeSound)];

但我不断收到错误消息说“预期的表达式”

我仍处于第一步,我正在关注的本教程适用于 ios 6 现在我正在使用 iOS 8,我找不到任何关于如何将推送通知实现到我的应用程序的完整教程。谁能指出我正确的方向?

【问题讨论】:

    标签: ios objective-c push-notification


    【解决方案1】:

    您错误地使用了 API。

    首先,推送通知 API 在 iOS 8.0 之后发生了变化。

    我的回答是假设您仍希望支持 iOS 7.x 及更高版本

    // Checks if the application responds to the API introduced in iOS 8.
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        // iOS 8 Support
        // Note that you pass a object of type UIUserNotificationSettings as parameter instead of the enums only.
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil]];
    } else {
        // Old API so use the same code from the tutorial
        [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    }
    

    【讨论】:

    • Still Gives me deprecated under this line of code ... [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];谢谢,我将继续使用你给我的代码以及我正在关注的教程,看看我是否可以让它一起工作:)
    • 如果您的目标是 iOS 8 及更高版本,它会发出警告,那么您无需担心 if 测试,只需立即关注 API。如果您使用的是 7 及更高版本,则需要 if 测试。
    • 啊,好吧,我这样做了,因为正如你所说,我的应用程序针对的是 iOS 8+...而且它的工作原理很吸引人...谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 2017-04-19
    • 2011-08-07
    • 1970-01-01
    相关资源
    最近更新 更多