接收重复推送通知ios9

我在iOS9中收到两次相同的推送通知,虽然它在iOS8中工作正常。

我已经使用下面的代码注册推送通知:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { // use registerUserNotificationSettings UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:( UIUserNotificationTypeSound | UIUserNotificationTypeAlert|UIUserNotificationTypeBadge) categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:setting]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { // use registerForRemoteNotifications [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert |UIRemoteNotificationTypeBadge)]; } #else // use registerForRemoteNotifications [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; #endif 

我在几个应用程序中遇到了这个问题,并且如果您调用registerUserNotificationSettings: ,则看起来像重复registerUserNotificationSettings:超过1次。

在这个答案的更多细节: https : //stackoverflow.com/a/35064911/4495995

这显然是一个苹果问题。 我在很多应用程序中遇到了同样的问题。 https://forums.developer.apple.com/thread/13414

从iOS 9每当您卸载并重新安装应用程序再次一个新的设备令牌已分配此可能是您收到多个推送通知的原因。

其实我是从一个论坛读出来的,他们提供的解决scheme是当你生成一个有效载荷时,增加一个额外的自定义随机值,这样每个有效载荷都有一些独特的价值。 在我的情况下在vb.net我使用DateTime.Now.ToString(“MMddyyyyHHmmssfff”)添加毫秒的唯一时间戳。 我希望自己的工作能够实现,但目前还没有经过testing。

我正在使用这个,这也是在Ios9也很好,请尝试。 将这添加到您的didFinishLaunchingWithOptions

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

调用的方法是

 - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{ NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]]; self.AppDeviceToken=[token stringByReplacingOccurrencesOfString:@" " withString:@""]; } 

首先检查你的数据库,并确保你没有得到设备令牌两次,很可能你有相同的令牌重复的条目。

其次,如果您在3到4天内安装/卸载应用程序,可能会收到两次甚至三次通知。

解决scheme:请尽量卸载应用程序一周,如果可能的话再次安装应用程序。

谢谢。