Interactive Notification not showing buttons ios 9.2 - ios

I believe I am creating interactive notifications properly, but I get notifications on springboard/lock screen w/out buttons. What am I doing wrong?
AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let notificationSettings: UIUserNotificationSettings! = UIApplication.sharedApplication().currentUserNotificationSettings()
/// don't want to constantly set/re-set when this function fires
if (notificationSettings.types == UIUserNotificationType.None){
let yesAction = UIMutableUserNotificationAction()
yesAction.identifier = "yesAction"
yesAction.title = "Yes"
yesAction.destructive = false
yesAction.authenticationRequired = false
yesAction.activationMode = UIUserNotificationActivationMode.Background
let noAction = UIMutableUserNotificationAction()
noAction.identifier = "noAction"
noAction.title = "No"
noAction.destructive = false
noAction.authenticationRequired = false
noAction.activationMode = UIUserNotificationActivationMode.Background
let stopAction = UIMutableUserNotificationAction()
stopAction.identifier = "stopAction"
stopAction.title = "Stop"
stopAction.destructive = true
stopAction.authenticationRequired = false
stopAction.activationMode = UIUserNotificationActivationMode.Background
let encouragementCategory = UIMutableUserNotificationCategory()
encouragementCategory.identifier = "GOCEncouragement"
encouragementCategory.setActions([yesAction, noAction, stopAction], forContext: UIUserNotificationActionContext.Default)
encouragementCategory.setActions([yesAction, noAction], forContext: UIUserNotificationActionContext.Minimal)
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: Set.init(arrayLiteral: encouragementCategory))
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}
return true
}
Somewhere else, called by application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings):
notificationArray.forEach({
let notification = UILocalNotification.init()
notification.fireDate = fireDay
notification.soundName = UILocalNotificationDefaultSoundName
notification.alertBody = $0["Encouragement"] as! String!
notification.repeatInterval = .Month
notification.category = "GOCEncouragement"
notification.userInfo = [ "UUID" : $0["UUID"] as! Int]
if #available(iOS 8.2, *) {
notification.alertTitle = "Choices Encouragement"
}
UIApplication.sharedApplication().scheduleLocalNotification(notification)
nextDay.day++
fireDay = NSCalendar.currentCalendar().dateByAddingComponents(nextDay, toDate: fireDay!, options: NSCalendarOptions.init(rawValue: 0))
})
To re-iterate the problem; notifications appear on schedule, but left-slide on lock screen doesn't reveal any buttons, nor does tugging down on home screen.
Any suggestions welcome!
Edit: thought it was simulator-only, but is on live 9.2 devices as well. Other iOS versions work as expected.

Related

Not able to play the music when recived LocalNotification, when app is in background state in swift

I'm scheduling up a Local Notification as an alarm for an app, where I'm setting music and vibration when local notification is received. When app is in foreground state everything is working fine, music is playing, vibration is there.
But when app is in background state only the default notification music i.e once only not in repeat and single vibration is occurring.
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
//show an alert window
var isSnooze: Bool = false
var soundName: String = ""
var index: Int = -1
if let userInfo = notification.userInfo {
isSnooze = userInfo["snooze"] as! Bool
soundName = userInfo["soundName"] as! String
index = userInfo["index"] as! Int
}
playSound(soundName)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
mainVC?.notification = notification
mainVC?.isFromNotificationDelegate = true
let nav = UINavigationController(rootViewController: mainVC!)
self.window?.rootViewController = nav
}
Register for notifications in AppDellegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Sound | .Alert | .Badge, categories: nil))
return true
}
Now, schedule your notification using following func, it will automatically play your custom sound whenever notification receieves
func localnotification (firedate:NSDate) {
var localNotification:UILocalNotification = UILocalNotification()
localNotification.fireDate = firedate
localNotification.alertBody = "time to woke up"
localNotification.soundName = "alarm.wav"
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}

Swift - How creating first time app launch local notification?

I have added these in didFinishLaunchingWithOptions
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
self.createLocalNotification()
And then, calling below function.
func createLocalNotification() {
let localNotification = UILocalNotification()
localNotification.fireDate = NSDate(timeIntervalSinceNow: 3)
// localNotification.applicationIconBadgeNumber = 1
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.userInfo = [
"id": "not_id0",
"message": "Check notification"
]
localNotification.alertBody = "Check notification"
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
To cancel this notification, I have tried below in didReceiveLocalNotification But still display notification every App launching.
let app:UIApplication = UIApplication.sharedApplication()
for oneEvent in app.scheduledLocalNotifications! {
let notification = oneEvent as UILocalNotification
let userInfoCurrent = notification.userInfo! as! [String:AnyObject]
let id = userInfoCurrent["id"]! as! String
if id == "not_id0" {
//Cancelling local notification
app.cancelLocalNotification(notification)
break;
}
}
How Can I create first time local notification? If someone explain It would be great.
You could use NSUserDefaults as such:
In didFinishLaunchingWithOptions: in your AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
if let haveShownFirstRunNotification = NSUserDefaults.standardUserDefaults().boolForKey("haveShownFirstRunNotification") {
if !haveShownFirstRunNotification {
createLocalNotification()
}
}
...
}
And in createLocalNotification:
func createLocalNotification() {
...
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "haveShownFirstRunNotification")
}
Add flag in in userDefaults and then check if
NSUserDefaults.standardUserDefaults().boolForKey("notified")
if not, open notification method and there place
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "notified")

UILocalNotification doesn't send

I'm trying to send a UILocalNotification like this:
func sendNotifiaction() {
let notification = UILocalNotification()
notification.userInfo = [
"Identifier": self.identifier!
]
notification.alertTitle = "Alarm!"
notification.alertBody = "test"
//notification.soundName = "Temporary-bleep-sound.aiff"
notification.category = "category"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
I tried to put a break point on this method and it is being called and run, but the notification doesn't sent at all.
Anyone has an idea why?
You gotta to register the Notification first
UIApplication.sharedApplication().cancelAllLocalNotifications()
let settings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
Here's my demo for Notification firing at a specific time
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
UIApplication.sharedApplication().cancelAllLocalNotifications()
let settings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
let localNotification1 = UILocalNotification()
localNotification1.alertBody = "Your alert message at 9:00 pm"
localNotification1.timeZone = NSTimeZone.defaultTimeZone()
localNotification1.fireDate = self.getNinePMDate()
UIApplication.sharedApplication().scheduleLocalNotification(localNotification1)
return true
}
func getNinePMDate() -> NSDate? {
let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let now: NSDate! = NSDate()
let date21h = calendar.dateBySettingHour(21, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)
return date21h
}
}
You should implement didReceiveLocalNotification in appdelegate and you have to show alert view manually with title and message as alertbody of your notification.

swift iOS localNotifications

I want to set local notifications for specific times, eg : 04:30 Am Everyday, but i cant figure it out. I am new to iOS development and all localNotifications tutorials just cover fireDate = NSDate()
Thank you!
Try this code in your function you want to fire local notification:
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let date = NSDate()
let dateComponents = calendar!.components([NSCalendarUnit.Day, NSCalendarUnit.WeekOfMonth, NSCalendarUnit.Month,NSCalendarUnit.Year,NSCalendarUnit.Hour,NSCalendarUnit.Minute], fromDate:date)
dateComponents.hour = 4
dateComponents.minute = 30
let notification = UILocalNotification()
notification.alertAction = "Title"
notification.alertBody = "Fire Fire Fire :v"
notification.repeatInterval = NSCalendarUnit.WeekOfYear
notification.fireDate = calendar.dateFromComponents(dateComponents)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
And remember to put this code in your AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Badge, .Alert], categories: nil))
return true
}

Interactive notification : buttons not showing (local or remote)

I followed many tutorials about interactive notifications and I don't know what is missing in my code. I can receive a simple push notification and I know that I have to swipe to make the buttons appear.
I have an iPhone 4S on iOS 9.2.1 and I use Mixpanel to send a notification.
I also tried local notification but it's not working either. (simulator 8.4, 9.2, iPhone 4S) I've got the message but no buttons.
Payload : { "aps" : { "category" : "RATING_CATEGORY", "alert" : "rate the app" }}
AppDelegate:
func registerForNotifications() {
if #available(iOS 8.0, *) {
let notificationActionRate :UIMutableUserNotificationAction = UIMutableUserNotificationAction()
notificationActionRate.identifier = "RATE_IDENTIFIER"
notificationActionRate.title = NSLocalizedString("Rate.Button.Title", comment: "Rate the app")
notificationActionRate.destructive = false
notificationActionRate.authenticationRequired = false
notificationActionRate.activationMode = UIUserNotificationActivationMode.Background
let notificationActionNotNow :UIMutableUserNotificationAction = UIMutableUserNotificationAction()
notificationActionNotNow.identifier = "NOT_NOW_IDENTIFIER"
notificationActionNotNow.title = NSLocalizedString("NotNow.Button.Title", comment: "Not now")
notificationActionNotNow.destructive = true
notificationActionNotNow.authenticationRequired = false
notificationActionNotNow.activationMode = UIUserNotificationActivationMode.Background
let notificationCategoryRating: UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
notificationCategoryRating.identifier = "RATING_CATEGORY"
notificationCategoryRating.setActions([notificationActionRate, notificationActionNotNow], forContext: UIUserNotificationActionContext.Default)
notificationCategoryRating.setActions([notificationActionRate, notificationActionNotNow], forContext: UIUserNotificationActionContext.Minimal)
let categories = Set([notificationCategoryRating])
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: categories))
UIApplication.sharedApplication().registerForRemoteNotifications()
} else {
UIApplication.sharedApplication().registerForRemoteNotificationTypes([.NewsstandContentAvailability, .Badge,.Sound,.Alert])
}
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
registerForNotifications()
return true
}
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
if identifier == "RATE_IDENTIFIER" {
let itunesLink = NSURL(string: "http://google.com")
UIApplication.sharedApplication().openURL(itunesLink!)
}
completionHandler()
}

Resources