Receiving duplicate push notification ios9

旧巷老猫 提交于 2019-11-29 02:32:58

问题


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.



来源:https://stackoverflow.com/questions/32840916/receiving-duplicate-push-notification-ios9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!