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 :)
Related
I'm currently trying to implementation Auth for iOS using Firebase. I've gotten it to work using the recaptcha method specified here: https://firebase.google.com/docs/auth/ios/phone-auth but still running into issues with the silent APN. I uploaded my APN key to the firebase project and added push notification to my swift project capabilities. I also added the following piece of code to my AppNameApp.swift file:
class Appdelegate : NSObject,UIApplicationDelegate{
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
UIApplication.shared.registerForRemoteNotifications()
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
}
}
I'm still unable to get it to work and I've been stuck for a few days now. Any help will be greatly appreciated!
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to make a request when the application enters the onBackground or onAppWillTerminate mode. I added an observer to AppDelegate method but did not work. Simply I want to send a request and push notification as a response. How can I do this? My request is not news or music, simple JSON.
`func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Fabric.with([Crashlytics.self])
GADMobileAds.configure(withApplicationID: "ca-app-pub-8446699920682817~7829108")
let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false]
//ONE SIGNAL
OneSignal.initWithLaunchOptions(launchOptions,
appId: "f5854e88-0b58-495f-8d3f-e7899202d",
handleNotificationAction: nil,
settings: onesignalInitSettings)
OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification;
OneSignal.promptForPushNotifications(userResponse: { accepted in
print("User accepted notifications: \(accepted)")
})
UIApplication.shared.statusBarStyle = .lightContent
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
mS.StartTimer()
//NotificationCenter.default.addObserver(self, selector:#selector(AppDelegate.applicationWillTerminate(_:)), name:NSNotification.Name.UIApplicationWillTerminate, object:nil)
NotificationCenter.default.addObserver(self, selector:#selector(AppDelegate.applicationWillEnterForeground(_:)), name:NSNotification.Name.UIApplicationWillEnterForeground, object:nil)
return true
}
func applicationWillTerminate(_ application: UIApplication) {
//mS.StartTimer()
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
self.mS.StartTimer()
if let vc = window?.rootViewController as? LiveScores{
vc.getLiveMatches(url : URL(string: "http://test.com/test/index.php/Service/lastLive"))
}
completionHandler(.newData)
}
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void){
completionHandler(UIBackgroundFetchResult.newData)
}
func applicationDidEnterBackground(_ application: UIApplication) {
if let vc = window?.rootViewController as? LiveScores{
vc.getLiveMatches(url : URL(string: "http://opucukgonder.com/tipster/index.php/Service/lastLive"))
}
mS.StartTimer()
}
func applicationWillEnterForeground(_ application: UIApplication) {
if let vc = window?.rootViewController as? LiveScores{
vc.getLiveMatches(url : URL(string: "http://opucukgonder.com/tipster/index.php/Service/lastLive"))
}
mS.StartTimer()
}
}`
Actually application is forcefully killed after 3-4 seconds when applicationWillTerminate is called.
so thats why API is not called.
One solution you can try is to add sleep at the end of the applicationWillTerminate function like this :
func applicationWillTerminate(_ application: UIApplication) {
//call API Here
// 5 is the number of seconds in which you estimate your request
// will be finished before system terminate the app process
sleep(5)
print("applicationWillTerminate")
}
If you want to contact the server when the app is going into the background, that is certainly possible. Implement UIApplicationDelegate.applicationDidEnterBackground. See Strategies for Handling App State Transitions for full details. This is a very common and supported operation and should be what you need.
However, it is not possible to contact the server when the app is killed in the background. applicationWillTerminate is generally never called in any case since iOS 4. Prior to iOS 4, there was no "background" mode. When the user pressed the home button, the app was immediately terminated after calling this method, and that's why it still exists. But it's all-but-useless in modern apps. (You can still get this ancient behavior by setting UIApplicationExitsOnSuspend in your Info.plist, but this does nothing to help your problem.)
When your application is killed in the background it is just killed. It is not woken up, and no methods are called on it. It is unceremoniously terminated and removed from memory. You cannot respond to this event. The same happens when the user force-quits your app from the app-switcher.
I'm a little curious about why you would want a push notification when the app goes into the background, and particularly when the app terminates. When I've seen people try to do this in the past, they often were trying to use push notifications to keep the app launched at all times. This is not possible. Even if you found a work-around to make it technically possible, it is explicitly forbidden by Apple.
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")
}
for testing I use OneSignal service for send push notification on my device and I handle it in AppDelegate in this way:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
OneSignal.initWithLaunchOptions(launchOptions, appId: “[app ID]”)//this method I register device on apple server
return true
}
func application(application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void){
print(“ARRIVED")
handleNotificationContent()// it’s not important for my question
}
My problem is that when I receive a notification and the app is in foreground , alert shows automatically and I don’t want to show it.
How do I solve this problem?
This code worked for me.
This is applicable for Swift 2.2 and Xcode 7.3.1
//Initialize One Signal using this code
OneSignal.initWithLaunchOptions(launchOptions, appId: oneSignalId, handleNotificationReceived: { (notification) in
//Put your business logic here like adding an alert controller or posting an NSNotification.
}, handleNotificationAction: { (nil) in
// This block gets called when the user reacts to a notification received
}, settings: [kOSSettingsKeyAutoPrompt : false, kOSSettingsKeyInAppAlerts: false])
//set kOSSettingsKeyAutoPrompt to false
You need to set the kOSSettingsKeyInFocusDisplayOption to None in initWithLaunchOptions, to disable the automatic display of inapp alerts.
OneSignal Api Reference
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