使用unregisterForRemoteNotifications后,registerusernotification设置不起作用 [英] registerusernotificationsettings not work after using unregisterForRemoteNotifications

查看:807
本文介绍了使用unregisterForRemoteNotifications后,registerusernotification设置不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用中使用诸如推送通知的禁用程序和启用程序之类的功能.我使用这样的代码.我也用sender.isOn

I want to use in my app something like disabler and enabler of push notifications. I use code like this. Also I activate notifications in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions with code from sender.isOn

if (sender.isOn) {

    self.notificationLabel.text = @"Notifications enabled";

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

} else {

    self.notificationLabel.text = @"Notifications disabled";

    [[UIApplication sharedApplication] unregisterForRemoteNotifications];

}

但是第一次使用unregisterForRemoteNotifications后,我的应用程序未使用registerForRemoteNotifications初始化通知.我不明白我在做什么错.

But after first use unregisterForRemoteNotifications my app not initialling notifications with registerForRemoteNotifications. I don't understand what I'm doing wrong.

推荐答案

解决方案:卸载应用程序并删除该代码!

我花了很多时间和3台坏掉的测试设备才能找到该设备.

It took me many hours and 3 broken test devices to track this one down..!

此行是:

[[UIApplication sharedApplication] unregisterForRemoteNotifications] //DON'T DO IT!

它使应用程序处于不稳定状态,无法调用registerForRemoteNotifications.

It puts the app in an unstable state where calling registerForRemoteNotifications no longer works.

此行同样讨厌:

[[UIApplication sharedApplication] registerForRemoteNotifications] //CALL ME ONCE!

您必须在每次运行应用程序时调用一次,并且只能调用一次.如果您碰巧打了两次电话,通知将神秘地中断.

You must call it once, and only once, per run of your app. If you happen to call it twice, notifications will mysteriously break.

这篇关于使用unregisterForRemoteNotifications后,registerusernotification设置不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆