关于IOS中UILocationNotification,为什么显示不出来本地通知

用的是xcode8,看下面的代码,还需要设置什么东西么,运行在iPhone 7上

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    UIWindow * window = [[UIWindow alloc]init];
    window.frame = [UIScreen mainScreen].bounds;
    self.window.rootViewController = [[ViewController alloc]init];

    [window makeKeyWindow];

    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    

    UILocalNotification * notification = [[UILocalNotification alloc]init];
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];//10秒后触发
    notification.repeatInterval = 2; //重复次数
    //设置通知的一些属性
    notification.alertBody = @"有一条新的消息请注意查收";//通知主题,外部显示的内容
    notification.alertTitle = @"新的消息";
    notification.applicationIconBadgeNumber = 66; //应用图标右上角显示消息的个数;
    notification.alertAction = @"打开应用";//待机界面的滑动动作提示
    notification.alertLaunchImage = @"Default";//通过点击通知打开应用时启动的图片,这里使用的是开启应用时的图片
    // notification.soundName = @""; 通知声音
    //设置用户信息
    notification.userInfo = @{@"id":@1,@"user":@"Yibo Wang"};//绑定到通知上的其他附加信息
    //调用通知
    [[UIApplication sharedApplication]scheduleLocalNotification:notification];
    return YES;
    }

ios

1个回答

你那个授权方法可能失效了,改成下面这个试试

                        // ios8后,需要添加这个注册,才能得到授权
            if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
                UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
                UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
                                                                                         categories:nil];
                [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
                // 通知重复提示的单位,可以是天、周、月
                notification.repeatInterval = NSCalendarUnitDay;
            }
wang_Bo_JustOne
Crazy_bananas 上面代码中有注册,运行在模拟器上没有通知,新旧的方法都用了
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问