Receiving duplicate push notification ios9

2019-02-02 23:03发布

问题:

I am receiving the same push notification twice in iOS9, although it is working fine in iOS8.

I have used the following code to register with push notifications:

#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

回答1:

I had this problem in several apps, and looks like duplicates appear if you call registerUserNotificationSettings: more than 1 time.

More details in this answer: https://stackoverflow.com/a/35064911/4495995



回答2:

It is apparently an Apple issue. I've faced the same issue many times across apps. https://forums.developer.apple.com/thread/13414



回答3:

From iOS 9 every time when you uninstall and than re-install the app again a new device token has assigned this might be the reason that you receives the multiple push notifications.

Actually I read out from one forum, they provide the solution that when you generating a payload that add one extra custom any random value so that each payload has some unique value. in my case in vb.net I am using DateTime.Now.ToString("MMddyyyyHHmmssfff") to add a unique time stamp with milliseconds. I hope its work I implemented this but not tested so far.



回答4:

I am using this and this is working fine in Ios9 also, please try it. Add this in your 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)];
}

Method for call is

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


回答5:

First check your database and make sure you didn't get the device token twice, quite possible that you have duplicate entries of the same token.

Secondly, If you install/uninstall the app within 3 to 4 days its possible that you get the notification twice or even thrice.

Solution: Please uninstall the app for a week if possible than install the app again.

Thank You.