最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[workaround]swift本地通知的alertTitle没生效

Swift crifan 2033浏览 0评论

swift的代码:

    func sendNotification(newMessage:Message){
        var notifTitle = ""
        var notifBody = ""
        (notifTitle, notifBody) = genMesageTitleBody(newMessage)
        print("notifTitle=\(notifTitle), notifBody=\(notifBody)")
        let localNotif = UILocalNotification()
        localNotif.fireDate = NSDate(timeIntervalSinceNow: 0.5)
        localNotif.timeZone = NSTimeZone.defaultTimeZone()
        if #available(iOS 8.2, *) {
            localNotif.alertTitle = notifTitle
        } else {
            // Fallback on earlier versions
        }
        localNotif.alertBody = notifBody
        localNotif.soundName = UILocalNotificationDefaultSoundName
        localNotif.alertLaunchImage = ""
        localNotif.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
        UIApplication.sharedApplication().scheduleLocalNotification(localNotif)
    }

效果:

其中的alertTitle已经设置了值了,但是没有生效。。

swift local notification alert Title not work

swift local notification alertTitle not work

最后感觉是:

好像对应本地通知的标题,是没法设置的

突然想到,可以借鉴别人的:换行,去实现显示两行内容

搜:

alertTitle  UILocalNotification

突然发现,对于代码:

        if #available(iOS 8.2, *) {
            localNotif.alertTitle = notifTitle
        } else {
            // Fallback on earlier versions
        }

意思是:

只有iOS 8.2才有

-》我现在的iOS 模拟器和真机,都是iOS 9的,所以没有alertTitle

ios – UILocalNotification’s alertTitle property doesn’t work – Stack Overflow

估计是:

要么是只有iOS 8.2才可以-》iOS9不行

要么是alertTitle只有针对于UIAlertView才能生效-》对于默认的UILocalNotification是没用的。

但是觉得很是诡异:

UILocalNotification Class Reference

“alertTitle

A short description of the reason for the alert.

Declaration

SWIFT

var alertTitle: String?

Discussion

Use this property to provide a short description of the reason for the alert. You may specify a string with the text you want to display or you may specify a string to use as a lookup key in your app’s Localizable.strings file. The default value of this property is nil.

Title strings should be short, usually only a couple of words describing the reason for the notification. Apple Watch displays the title string as part of the short look notification interface, which has limited space.

Availability

Available in watchOS 2.0 and later.”

说的是:

watchOS 2.0及以后,都有,但是没提及iOS哪个版本

而Xcode中查看源代码看到的是:

    @available(iOS 8.2, *)
    public var alertTitle: String? // defaults to nil. pass a string or localized string key

20156339: UILocalNotification alertTitle should appear on the lock screen—current usage is inconsistent and confusing · Issue #1746 · lionheart/openradar-mirror · GitHub

ios – Custom title for UILocalNotification – Stack Overflow

-》

本身设计就是title不允许改,就只是app的name

如果你找到办法改了,那孤寂Apple都不会通过的app审核

[总结]

UILocalNotification的alertTitle

-》对于UIAlertView好像是有效的-》可以自定义本地通知的标题

-》对于普通的UILocalNotification,是没用的-》通知的标题始终都是app的名字

-》官方也不允许不推荐修改-》目的应该是让用户明白是哪个app发送的通知

-》万一你找到办法改了标题,可能会因此app无法通过审核

-》此处再去尝试,看看能否显示多行内容的方式实现通知的标题

果然是可以的,通过代码:

    func sendNotification(newMessage:Message){
        var notifTitle = ""
        var notifBody = ""
        (notifTitle, notifBody) = genMesageTitleBody(newMessage)
        print("notifTitle=\(notifTitle), notifBody=\(notifBody)")
        UIApplication.sharedApplication().cancelAllLocalNotifications()
       
        let localNotif = UILocalNotification()
        localNotif.fireDate = NSDate(timeIntervalSinceNow: 0.1)
        localNotif.timeZone = NSTimeZone.defaultTimeZone()
//        if #available(iOS 8.2, *) {
//            localNotif.alertTitle = notifTitle
//        } else {
//            // Fallback on earlier versions
//        }
//        localNotif.alertBody = notifBody
        localNotif.alertBody = notifTitle + "\n" + notifBody
        localNotif.soundName = UILocalNotificationDefaultSoundName
        localNotif.alertLaunchImage = ""
        localNotif.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
        UIApplication.sharedApplication().scheduleLocalNotification(localNotif)
    }

效果如下:

讨论组中发送消息:

和单个人私聊:

私聊中发送图片:

私聊中发送文件:

转载请注明:在路上 » [workaround]swift本地通知的alertTitle没生效

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
91 queries in 0.209 seconds, using 22.10MB memory