UILocalNotification无法正常工作 [英] UILocalNotification not working properly

查看:135
本文介绍了UILocalNotification无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码。我必须为localNotification添加任何内容。?有什么建议么。提前致谢。我阅读了很多本地通知教程。我不知道错过了什么。

This is my code.is there anything that i have to add for the localNotification.? Any suggestions. Thanks in advance. I read lots of local notification tutorials. I have no idea what is missed.

var localNotifications : UILocalNotification!

    func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {

        localNotifications  = UILocalNotification()
        localNotifications.fireDate = NSDate(timeIntervalSinceNow: 20)
        localNotifications.timeZone = NSTimeZone.defaultTimeZone()
        localNotifications.alertBody = "Check it out!"
        localNotifications.soundName = UILocalNotificationDefaultSoundName
        localNotifications.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
        UIApplication.sharedApplication().scheduleLocalNotification(localNotifications)

}

 func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {

        println("Alvin Notification recieved")

        if application.applicationState == UIApplicationState.Active
        {
            // show alertView

            Utilities.sharedInstance.alertThis("Hello", message: "Hey")
        }
        else if application.applicationState == UIApplicationState.Background
        {
            UIApplication.sharedApplication().presentLocalNotificationNow(localNotifications)
        }
        else if application.applicationState == UIApplicationState.Inactive
        {
            UIApplication.sharedApplication().presentLocalNotificationNow(localNotifications)
        }

        application.applicationIconBadgeNumber = 0
    }


推荐答案

来自本地和远程通知编程指南


在iOS 8及更高版本中,使用本地或远程通知的应用必须注册他们打算提供的通知类型。然后,系统使用户能够限制应用程序显示的通知类型。如果未对您的应用启用任何这些通知类型,系统不会标记图标,显示警报消息或播放警报声音,即使它们已在通知有效负载中指定。

In iOS 8 and later, apps that use either local or remote notifications must register the types of notifications they intend to deliver. The system then gives the user the ability to limit the types of notifications your app displays. The system does not badge icons, display alert messages, or play alert sounds if any of these notification types are not enabled for your app, even if they are specified in the notification payload.

如指南所示,Apple显示的示例代码为:

As shown in the guide, the sample code that Apple shows is:

    UIUserNotificationType types = UIUserNotificationTypeBadge |
             UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

UIUserNotificationSettings *mySettings =
            [UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

这篇关于UILocalNotification无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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