call rest-api whenever app is opened through a push notification [closed] - ios

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")
}

Related

Pusher Beams Critical Notifications [closed]

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 :)

Access Notification Payload if application is launched by local notification

How can I get notified and access the notification payload if a user clicks on a local notification while the app is in the suspended state and launches the application?
Whenever my iOS app is opened from the background state the UNNotification response function is called. The local notification launched by key has been deprecated and I can't figure out how to determine if the app has been launched by a local notification. I need to use the notification payload to populate the view controller properly.
I am using this function to get the notification response while the app is in the background state:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive
response: UNNotificationResponse, withCompletionHandler completionHandler:
#escaping () -> Void) {
setup view here using payload
}
The problem is likely that you are not configuring the UNUserNotificationCenterDelegate early enough. You need to do that very early, as in didFinishLaunching, so that the runtime knows where to send didReceive. This is how I do it:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self.notifHelper
return true
}

Local notification not calling didReceiveRemoteNotifications in swift 4 [duplicate]

This question already has answers here:
didReceiveRemoteNotification not called, iOS 10
(9 answers)
Closed 4 years ago.
I have the following appDelegate:
import UIKit
import CoreData
import UserNotifications
#available(iOS 11.0, *)
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
{
var window: UIWindow?
...
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])
{
print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}
}
I am using ios11 and swift 4.1 but the notification gets fired off correctly. But when I tap it I can't grab it in the app. Why? I am using xcode 9.4.1. Thanks.
The app is in the background and when the notification is tapped the app opens up.
Hello #cdub You should use usernotificatioCenter
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
// do code here to show notification alert in for-ground mode
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
//your notification handling code here
}

iOS sends the notification object to UserNotificationCenter and to didFinishLaunchingWithOptions

When the app is closed and the user taps on a notification:
1) didFinishLaunchingWithOptions is called with a remoteNotification object. I set the rootViewController here as expected
2) userNotificationCenter(_, didReceive, withCompletionHandler) is also called.
The thing is, I don't want to do anything on step 2 because I already handled the notification on step 1). But, if the app was in the foreground or background, I do want to handle the notification in step 2). I don't know how to differentiate these two cases.
I think they are for separate concerns.
That is userNotificationCenter(_, didReceive, withCompletionHandler) is for handling Responses. What action did the user click, did they just swipe to clear it? Did they respond to the message right there?
But the didFinishLaunchingWithOptions is there only for receiving a callback, it's just there to tell you that the app was launched through the notification after it was killed/suspended...be aware...and here's your chance to do launch/do whatever you usually do during app launches
You can use a flag variable in AppDelegate class.
The initial value is false, with the name isAppActive.
This variable will set to true whenever func applicationDidBecomeActive(_ application: UIApplication) is called.
So, you can check if app already finished the first setup/initialization or not. AppDelegate instance is a shared instance so you can check it anywhere at runtime.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool is called only once when app is completely loaded into RAM. After that, you can check isAppActive value. Ex:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void)
if isAppActive {
// Do your logic here
}

How to send a push notification after a button is tapped? [closed]

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

Resources