推送通知在 iOS 9 中不起作用

Push notifications are not working in iOS 9

我已将我的设备升级到 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 上运行良好。

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

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

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

请试用此代码,因为它适用于我的 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. 在下面的函数中收到推送通知令牌,并且没有返回任何错误,这很好地发送到服务器(LAMP 服务),该服务器正在向设备令牌发送 APNS 推送通知。
  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);
        }
    

如果对post您更新的代码有任何帮助,希望得到很好的解释,以便我们可以帮助您。

如果您使用 Diawi 安装应用程序,那么推送通知在 IOS 9..

中不起作用

您安装应用程序 Xcode..

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

*Just enable this from your X-code*

了解更多详情 - iOS 9 Push Notification Tutoriyal