【问题标题】:Push notifications are not working in iOS 9推送通知在 iOS 9 中不起作用
【发布时间】:2015-12-19 12:20:00
【问题描述】:

我已将我的设备升级到 iOS 9 并将我的 Xcode 环境升级到 7.0 贝塔。推送通知在 iOS 9 中不起作用?

这是我的代码:

  float ver = [[[UIDevice currentDevice] systemVersion] floatValue];

    if(ver >= 8 && ver<9)
    {
        if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
        {
            [[UIApplication sharedApplication] registerForRemoteNotifications];
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

        }
    }else if (ver >=9){

        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

        [[UIApplication sharedApplication] registerForRemoteNotifications];


    }
    else{
        //iOS6 and iOS7 specific code
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert];
    }

推送通知在使用 Xcode 6.4 构建的 iOS 8 到 8.3 上运行良好。

【问题讨论】:

  • 我遇到了同样的问题。如果您找到解决方案,请告诉我
  • 有人找到解决方案吗?我遇到了同样的问题。
  • 我也遇到了同样的问题..
  • 我遇到了同样的问题,推送通知对我有用,即使在 iOS 9 和 xcode 7 上也是如此,上周我升级到 El Capitan 和 XCode 7.1 和 iOS 9.1,然后它们停止工作,我只测试了我的开发环境,但会测试其他人。如果有人有任何想法,请尽快提供解决方案。我尝试使用 Ray 的教程重新生成证书,该教程一直有效,但这次除外

标签: push-notification apple-push-notifications ios9 xcode7


【解决方案1】:

在您的项目中添加code 并创建证书和临时配置文件 -

*Just enable this from your X-code*

更多详情-iOS 9 Push Notification Tutoriyal

【讨论】:

  • 在将项目从 swift 2 升级到 3 后发生在我身上。令人讨厌的是 -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 仍然被调用- 除非选中此选项,否则您将不会收到通知。
  • 帮我解决了这个问题。在迁移到 XCode 8 时,我还将项目从 Swift 2.0 升级到 3.0。
  • 是的,Swift 3.0 和 X-Code 8 也一样。
【解决方案2】:

我遇到了同样的问题。我通过创建 Ad-Hoc Profile 解决了这个问题。我注意到当我使用 开发配置文件 时没有发生推送。我注意到的另一件事是 Device Token 在每次全新安装时都会更改,这有点奇怪,因为我们必须为每个新实例更新服务器。

您可以阅读这篇文章以获得更好的理解: iOS 9 Push Notifications

【讨论】:

  • 我知道设备令牌在全新安装时会不断变化。那不是问题。我每次登录时都会发送 device_token。
  • 您是否尝试过使用临时配置文件?对我来说,开发证书和分发证书似乎都在起作用。
  • 是的。我努力了。正如我上面提到的,您知道通知在 iOS 8 上使用相同的配置文件,但不适用于 iOS 9。
【解决方案3】:

在 didFinishLaunchingWithOptions 中编写此代码

 UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                UIUserNotificationTypeBadge |
                                                UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                         categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];

并将此方法添加到 AppDelegate,就像我用于解析推送通知服务一样

 - (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];
}

【讨论】:

    【解决方案4】:

    请尝试此代码,因为它适用于我的 IOS 9 应用程序

    在 AppDelegate DidFinishLaunchWithOption 中编写代码

    if([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:  [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil]];
    
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
       #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge |    UIUserNotificationTypeSound)]; #endif
    }
    

    确保在服务器上为推送通知检查以下项目

    1. 从发起者处选择证书和私钥生成的 PEM 文件
    2. Apple 服务已启用并正确指向 Sandbox/Live
    3. 在下面的函数中接收到推送通知令牌,并且没有返回任何错误,这很好地发送到正在向设备令牌发送 APNS 推送通知的服务器(LAMP 服务)。
    4. 使用推送发送到令牌检查服务器中的 Apple APNS 响应代码。
    5. 在设置中启用推送通知,就像您在“|”中做任何错误一样符号代码,您可能会看到 Apple 常规通知设置中未显示的声音、徽章选项

      -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
          {
              NSString *devicetokenString = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""] stringByReplacingOccurrencesOfString: @">" withString: @""] stringByReplacingOccurrencesOfString: @" " withString: @""]; 
      
              DLog(@"Did Register for Remote Notifications with Device Token DATA (%@) \n STRING token (%@)", deviceToken,devicetokenString);
      
              //If Same token received again dont take any action else save in NSUserdefaults or system and send to server to register against this device to send push notification on the token specified here.
          }
      
      -(void)application:(UIApplication *)application    didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 
          {
              DLog(@"Did Fail to Register for Remote Notifications");
              DLog(@"%s:%@, %@",__PRETTY_FUNCTION__,error, error.localizedDescription);
          }
      
      -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
          {
              DLog(@"Did Receive for Remote Notifications with UserInfo:%@", userInfo);
          }
      

    如果有任何帮助发布您更新的代码,希望这能得到很好的解释,以便我们为您提供帮助。

    【讨论】:

      【解决方案5】:

      如果您使用 Diawi 安装应用程序,而不是推送通知在 IOS 9 中不起作用..

      您通过 Xcode 安装应用程序..

      【讨论】:

      • @chandresh : 不,我不是通过 Diawi 安装的。
      • 我使用 Diawi 安装 iOS9 应用程序并且推送通知工作正常。
      猜你喜欢
      • 2016-03-22
      • 1970-01-01
      • 2015-06-28
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      相关资源
      最近更新 更多