Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I currently have an iOS App that is already set up to receive push notifications. What I'm trying to do is, after a user presses the 'Join' button, I want to send them a "Welcome" push notification. Any clue on how to do this?
It's pretty easy.
AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Sound, .Badge], categories: nil))
return true
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
print("Local notification received (tapped, or while app in foreground): \(notification)")
}
Then in your action:
#IBAction func welcomeMe(sender: AnyObject) {
let notification = UILocalNotification()
notification.alertBody = "Welcome to the app!" // text that will be displayed in the notification
notification.fireDate = NSDate(timeIntervalSinceNow: 2)
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["title": "Title", "UUID": "12345"]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
Now if the app is in the background, you see an Push notification. If it's in the foreground then your didReceiveLocalNotification fires instead. Tapping on the notification launches your app to the foreground and fires didReceiveLocalNotification also.
On YouTube, Jared Davidson offers some great iOS tutorials.
He has two on notifications:
This one is just what you need:
https://www.youtube.com/watch?v=tqJFJzUPpcI
...and there's one for remote notifications (not with a button)
https://www.youtube.com/watch?v=LBw5tuTvKd4
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
This post was edited and submitted for review 4 days ago.
Improve this question
Can't get it to work I'm new to swift
Using pusher beams To send Push remote notification to app.
{
"interests":[
"hello"
],
"apns":{
"aps":{
"alert":"This is a Critical Alert!",
"badge":1,
"sound":{
"critical":1,
"name":"your_custom_sound.aiff",
"volume":1.0
}
}
}
}
IF trying I'm getting an error
{
"error": "Unprocessable Entity",
"description": "`apns.aps.sound`: Invalid type. Expected: string, given: object"
}
Appdelegate :
self.pushNotifications.registerForRemoteNotifications(options: [.alert, .sound, .badge, .criticalAlert, .carPlay])
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
self.pushNotifications.registerDeviceToken(deviceToken)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
self.pushNotifications.handleNotification(userInfo: userInfo)
print(userInfo)
}
Just to receive an Critical Alert
I hope someone can help me out :)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
For my statistics dashboard inside the backend i need to call a rest-api via alamofire that informs the backend that the user opened the app through a push notification.
How can I achieve that?
I worked with local notifications so i will give you an example about what i do. I dont know if its the same way for push notifications.
What i do is to set appdelegate to conform to User Notifications delegate
1: Import
import UserNotifications
2: Add protocol
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
3: Notification Center Instance
var notificationCenter: UNUserNotificationCenter!
4: Initialize and set delegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self
return true
}
5: NotificationCenter response
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
print("notification pressed")
}
I am working Push notification and i have done all steps to setup push notification.
i can able to receive notification when application in background but when application in foreground its landing on didReceiveRemoteNotification but its not firing.
here my code in AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Alert, .Sound], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
if application.applicationState == UIApplicationState.Active {
let localNotification = UILocalNotification()
let date = NSDate(timeIntervalSinceNow: 10)
localNotification.fireDate = date
let timeZone = NSTimeZone.localTimeZone()
localNotification.timeZone = timeZone
localNotification.alertBody = "Sample Notification Body"
localNotification.userInfo = userInfo
print(localNotification)
localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
}
What mistake i am doing ??
Thanks in advance
What I suspect is, you're trying to show the remote notification when the app is active using local notification.
Well, either its local notification or remote, you can't see any UI alert when the app is active, instead the delegate methods for local or remote notification are called accordingly.
However iOS 10 adds the ability to view it inside the app. Here's the discussion: https://stackoverflow.com/a/37844312/593709
For showing any alert on pre-iOS-10, while your app is active, you need to use UIAlertController or some other implementation like MPNotificationView.
When the application is in Foreground and running, it does not show notification banner. Banner alert is only shown when Application is either in background(inactive) or is not running at all. The delegate method
application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)
is called for Remote(Push) Notification and
application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)
is called for local notification.
You can use any 3rd party library bryx-inc/BRYXBanner to show a banner for your notification alert message . The delegate method didReceiveRemoteNotification can be used to update badge icon and show message in banner view.
I am implementing push kit and receiving remote notification in delegate method "didReceiveIncomingPushWithPayload" but notification center is not showing that message. I have checked in my device "Notification-> app" allow notification and show in notification center options are enabled.
If you are using below method.
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!)
Then, pushkit helps in getting silent push notification, that remote notification would not come in notification center like simple APNS.
From this method when you receive remote notification with desired information now you have to schedule UILocalNotification which will come in notification center.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let notification:UILocalNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {
}
}
Even you can keep UILocalNotification object in NSUserDefault, So you can retrieve it in didFinishLaunchingWithOptions in case user device is getting restart and notification information is very crucial.
I'm doing this app that needs to send notifications when an event is coming.
Everything is working but when the app is closed and i open through the notification it doesn't fire how it is supposed to.
My App Delegate didFinishLaunchingWithOptions and didReceiveLocalNotification
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
createCopyOfDatabaseIfNeeded()
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound |
UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
if let options = launchOptions {
let value = options[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification
if let notification = value {
self.application(application, didReceiveLocalNotification: notification)
}
}
return true
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
// Do something serious in a real app.
println("Received Local Notification:")
println(notification.alertBody)
if notification.alertAction == "editList" {
NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: nil);
}
}
In my main tab controller:
override func viewDidLoad() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleModifyListNotification", name: "modifyListNotification", object: nil)
super.viewDidLoad()
self.repaint()
}
func handleModifyListNotification(){
dispatch_async(dispatch_get_main_queue(), {
// Show the alert
})
}
The main objective is when a notification is pressed with the app closed, it opens, executes didFinishLaunchingWithOptions and checks for UILocalNotification in launchOptions and calls didReceiveLocalNotification.
ps: i know with debug that the notifications is being well received, the system is just not calling the didReceiveLocalNotification method, like it should.
1ยบ Edit
I'm really calling the didReceiveLocalNotifaction method because i just tried this and it worked
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
// Do something serious in a real app.
println("Received Local Notification:")
println(notification.alertBody)
if notification.alertAction == "editList" {
var xpto = UIAlertView()
xpto.title = notification.alertAction!
xpto.show() NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: nil);
}
}
So i gess this must be a timing issue. I really want my tab controller to react to this notification.
NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: nil);
If I'm not mistaken, -didReceiveLocalNotification: is only called when the app is in active state and being used.
If you're attempting to launch the app from a background state, I would suggest using -didFinishLaunchingWithOptions: and querying your options.
For this reason, maybe have your notification handling code in a separate method and have it called from both -didReceiveLocalNotification & -didFinishLaunchingWithOptions ?