iOS handling Push notification tap when app is active - ios

How can I handle push notification tap when app is active?
didReceiveRemoteNotification userInfo: [AnyHashable : Any] is called when iOS device receives a push notification and same function is called when user taps on the notification. How can I differentiate in this function how this function is called? I'm using OneSignal of push notification in case it's necessary to understand the problem.

OneSingal has closures which informs you about notifications and user actions. This is how I use that
func initOneSignalNotifications(withLaunchOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) {
let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false]
OneSignal.initWithLaunchOptions(launchOptions, appId: Constants.oneSignalKey, handleNotificationReceived: { (receivedNotification) in
//Notification is received
}, handleNotificationAction: { (notificationResult) in
//Notification was tapped
}, settings: onesignalInitSettings)
OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification;
OneSignal.promptForPushNotifications(userResponse: { accepted in
FileHandler.log(message: "Notification permission granted: \(accepted)", tag: .application, logLevel: .info)
})
}
Here inFocusDisplayType means when your app is open, OneSignal will still display a notification.

You can handle click in, If your target is above iOS 10.
func userNotificationCenter ( _ center : UNUserNotificationCenter, didReceive
response : UNNotificationResponse, withCompletionHandler completionHandler :
#escaping () -> Void ) {
// perform notification received/click action as per third party SDK as per their document
}
}
else, You need to manage it with flags in didReceiveRemoteNotification userInfo: [AnyHashable : Any].

Related

Swift iOS app receive push notification when app is inactive and run code

Platform
Swift 5
iOS 13+
xCode 11
Node v14.2.0
Firebase/Firestore latest
Setting
Alice send push notification to Bob, while Bob's phone is .inactive or .background. Bob's phone should get notification and immediately trigger code.
Problem
This question has plenty of answers, but most of what I can find revolves around hacking the PushKit and CallKit native API to send .voIP pushes. Per this question (iOS 13 not getting VoIP Push Notifications in background), Apple no longer allow you to send .voIP pushes w/o triggering CallKit's native phone ring routine.
On iOS side, I have the following bits in AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
registerForPushNotifications()
}
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void)
{
print(">>> I would like this to be triggered even if the app is asleep")
switch application.applicationState {
case .active:
print(">>>>>>> the app is in [FOREGROUND]: \(userInfo)")
break
case .inactive, .background:
print(">>>>>>>> the app is in [BACKGROUND]: \(userInfo)")
break
default:
break
}
}
func registerForPushNotifications() {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter
.current()
.requestAuthorization(options:[.alert, .sound, .badge]) {[weak self] granted, error in
guard granted else { return }
self?.getNotificationSettings()
}
}
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
guard settings.authorizationStatus == .authorized else { return }
Messaging.messaging().delegate = self
DispatchQueue.main.async {
// Register with Apple Push Notification service
UIApplication.shared.registerForRemoteNotifications()
/// cache token client side and save in `didRegisterForRemoteNotificationsWithDeviceToken`
if let token = Messaging.messaging().fcmToken {
self.firebaseCloudMessagingToken = token
}
}
}
}
//#Use: listen for device token and save it in DB, so notifications can be sent to this phone
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
if (firebaseCloudMessagingToken != nil){
self.updateMyUserData(
name : nil
, pushNotificationToken: firebaseCloudMessagingToken!
)
}
}
func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
///print(">>> Failed to register: \(error)")
}
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
// #NOTE: this fires when the app is open. So you can go the call screen right away
let payload = notification.request.content.userInfo as! [String:Any?]
let type = payload["notificationType"]
print(">> this fires if the app is currently open")
}
/// #NOTE: we are using backward compatible API to access user notification when the app is in the background
/// #source: https://firebase.google.com/docs/cloud-messaging/ios/receive#swift:-ios-10
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping () -> Void) {
print(" this fires when the user taps on the notification message")
}
On the server/Node.js side, I send push notification this way:
// Declare app push notification provider for PushKit
const _ApnConfig_ = {
token: {
key : fileP8
, keyId : "ABCDEFG"
, teamId: "opqrst"
},
production: false
};
var apnProvider = new apn.Provider(_ApnConfig_);
exports.onSendNotification = functions.https.onRequest((request, response) => {
var date = new Date();
var timeStamp = date.getTime();
const deviceTok = "..."
var recepients = [apn.token( deviceTok )]
const notification = new apn.Notification();
notification.topic = "com.thisisnt.working"
notification.body = "Hello, world!";
notification.payload = {
from: "node-apn"
, source: "web"
, aps: {
"content-available": 1
, "data" : { "custom_key":"custom value", "custom_key_2":"custom value 2" }
}
};
notification.body = "Hello, world # " + timeStamp;
return apnProvider.send(notification, recepients).then(function(res) {
console.log("res.sent: ", res.sent)
console.log("res.failed: ", res.failed)
res.failed.forEach( (item) => {
console.log(" \t\t\t failed with error:", item.error)
})
return response.send("finished!");
}).catch( function (error) {
console.log("Faled to send message: ", error)
return response.send("failed!");
})
})
Both are pretty standard. I have set the content-availabe to 1. Right now the messages are coming through and displayed by Apple Push Notification center, they're just not triggering the block with didReceiveRemoteNotification as intended.
You need to enable the background mode - remote notifications capability.
To receive background notifications, you must add the remote notifications background mode to your app. In the Signing & Capability tab, add the Background Modes capability, then select the Remote notification checkbox.
Enabling the remote notifications background mode:
For watchOS, add this capability to your WatchKit Extension.
Source: Pushing Background Updates to Your App | Apple Developer Documentation

launchOptions always nil when launching from a push notification

I'm sending push notifications from a Django app (using django-push-notifications) to an iOS app. The app targets iOS 13 and I'm running it on an iPhone 7 running iOS 13.3.1. I'm debugging in Xcode 11.3.1
I'm trying two different methods to send the notification from the Django side:
Method 1:
devices.send_message(message={"title" : title, "body" : message}, thread_id="events", extra={"foo": "bar"})
Method 2:
devices.send_message("[will be overwritten]", extra={
"aps": {
"alert": {
"title": "Bold text in the notification",
"body": "Second line in the notification"
},
"sound": "default",
},
"foo": "bar"
})
As far as I can tell, both methods should result in a payload which looks like Method 2.
I'm debugging by doing the following:
Set "wait for executable to be launched" in my device scheme
Build and run in Xcode
Ensure app has been killed in the task switcher
Trigger sending of remote notification
Tap on received notification to launch app
No matter what I do, launchOptions is always nil. I've tried setting a breakpoint to inspect the variables. I've tried using os_log to log to the console if launchOptions is not nil, and I've tried triggering an alert (following advice from this question) to rule out Xcode debugger interference. It's always nil.
My AppDelegate currently looks like this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let notificationOption = launchOptions?[.remoteNotification]
let alert = UIAlertController(title: "Your title", message: notificationOption.debugDescription, preferredStyle: .alert)
let cancel = UIAlertAction(title: "Cancel", style: .default, handler: { action in
})
alert.addAction(cancel)
DispatchQueue.main.async(execute: {
application.windows.first!.rootViewController?.present(alert, animated: true, completion: nil)
})
return true
}
The alert triggers, but the alert content simply reads "nil".
I can't figure out what's missing. It's possible that my notification payload isn't exactly what I think it is (I've asked on the Github page for django-push-notifications to confirm if there's an issue on that end). It's also possible I've missed a step in setting up remote notifications, but I do reliably receive the notifications and they display as I expect, so they seem to be working.
Any advice greatly appreciated!
In iOS 13.0 When the app is killed, if you tap on notification, would like to open the app and get hold of notification payload. Here is how you do it.
Please check for connectOptions under sceneDelegate
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
//look for remote notification response
if let response = connectionOptions.notificationResponse{
print(response.notification.request.content.userInfo)
}
guard let _ = (scene as? UIWindowScene) else { return }
}
I didn't find a solution to this issue, but I found a workaround. I still have no idea why launchOptions was always nil, but I've been able to access the payload by doing the following:
In AppDelegate.swift:
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UNUserNotificationCenter.current().delegate = self
return true
}
...
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
let actionIdentifier = response.actionIdentifier
switch actionIdentifier {
case UNNotificationDismissActionIdentifier: // Notification was dismissed by user
// Do something
completionHandler()
case UNNotificationDefaultActionIdentifier: // App was opened from notification
// Do something
completionHandler()
default:
completionHandler()
}
}
If I then set a breakpoint in userNotificationCenter, I can dig out the notification payload:
It seems that after changes in iOS 13 we don't have to process notifications in didFinishLaunchingWithOptions function.
We can just use:
extension AppDelegate: UNUserNotificationCenterDelegate{
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let aps = userInfo["aps"] as? [String: AnyObject] {
// Do what you want with the notification
}
completionHandler()
}
}
It works for any scenario when the user clicks on a notification.

Show fcm notification if meet a condition with swift

I am using firebase push notification, where my app is subscribed to a topic, all is good. But I want to know if it is possible to show the notification if pass a notification. this is my scene:
local_user_id = 10
var payload = {
notification: {
title: "hi",
body: "this is a notification",
sound: "default"
},
data: {
user_id: "1",
message: "you should pay $3020.25"
}
};
1) control if user is_login (true/false)
2)get the message data of notification and check:
if (payload.data.user_id = local_user_id && is_login){
show_notification()
}
3) show notification
Actually I only have the notification and no more, I am new with firebase, this is my code:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
}
application.registerForRemoteNotifications()
FirebaseApp.configure()
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
let dict = userInfo["aps"] as! NSDictionary
let message = dict["alert"]
print("response")
print(message)
}
I don't know how to do that what I want, is it posible?
thanks in advance
You can not control show hide notification in your application. You can put some logic on the backend side, whether this notification should be displayed or not. Nevertheless, I have workaround below possible way.
Use a silent push. Then trigger local notifications. Note: Silent
push isn't always reliable.
So just include content-available: 1 in your payload as shown
below to get a silent notification.it will act as silent notification.
Also in Info.plist should have UIBackgroundModes set to
remote-notification
but it'll be limited to Running and background mode only. you won't be able to receive or handle it if content-available is set to 0 while your app is offline
If you are trying to just present the notification to the user while the app is running in the foreground, you would need to have your AppDelegate conform to the UNUserNotificationCenterDelegate. This is because when the application is running, the notifications will be presented to the UNUserNotificationCenter shared object.
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void {
let content = notification.request.content
if content.data["user_id"] == local_user_id && is_login {
completionHandler(.alert)
} else {
completionHandler([])
}
}
}
You would want to make sure that the completionHandler is executed at some point in this block, because this is the handler that does the presentation of the notification. If you want a silent notification, you can use completionHandler([]) to silence the alert. Other possible options for the completionHandler are available here.

Reloading the push notification badge in the background

I am creating a notification service using swift3 in xcode 10.
The problem now is that when a push notification comes in the background (even when the app is closed), the badge does not increase at first, but increases by 1 from the second push notification.
Furthermore, when I enter the app and come back in the background, the number of badges will be normal, but the above problem will happen again.
I tried to check the problem through delay or local notifications, but I have not been able to figure out what the problem is.
Below are the notifications related to the notifications within the AppDelegate. Push Notification Click event also works normally.
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate, NaverThirdPartyLoginConnectionDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert,], completionHandler: {(granted, error) in
if (granted)
{
application.registerForRemoteNotifications()
}
else{
}
})
return true
}
...
...
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.badge, .alert, .sound])
UIApplication.shared.applicationIconBadgeNumber = UIApplication.shared.applicationIconBadgeNumber + 1
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
print("userInfo: \(response.notification.request.content.userInfo)")
var userInfo:[AnyHashable: Any]?
let pushId:Int32 = userInfo?["uid"] as! Int32
self.moveView(pushId)// My app load method
}
}
Running app in the background state is just a brief stop on the way to the app being suspended. While suspended, an app remains in memory but does not execute any code. For this reason your code is not executing and thus badge value does not update. See these below link to about application state and background execution.
Application State
Background Execution
So better approach to solve this problem is to send send badge value inside of push notification payload. e.g
{
"aps" : {
"alert" : {
"title" : "Game Request",
"body" : "Bob wants to play poker",
"action-loc-key" : "PLAY"
},
"badge" : 5
},
"acme1" : "bar",
"acme2" : [ "bang", "whiz" ]
}
See this link to create remote notification payload
Creating the Notification Payload
Don't increase badge value programmatically unless you need to show badge for local notification. If you want to execute code on background while push notification receive, use VoIP push notification which has few restriction e.g app must be related VoIP services.
I recommend to change the push notification payload.
Thanks.

remote push Notification when app is in background swift 3

I have an app that receives remote push notification.
I have implemented didReceiveRemoteNotification in this way:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
print("PUSH NOTIFICATION is coming")
let state: UIApplicationState = UIApplication.shared.applicationState
let inBackground = state == .background
let dizionario = userInfo["aps"]! as! NSDictionary
let alert = dizionario["alert"]! as! String
print(alert)
if(!inBackground){
print("APP IN FOREGROUND")
//show alert view and the if user tap 'ok' show webview
}
else{
print("APP IS BACKGROUND")
//SHOW WEBVIEW
}
}
In both cases(when app is in foreground and background) I have to show webview that add like child to root view(tabbar controller) but if app is in foreground then I have to show , before , an alert view.
My problem is that if app is in foreground I haven't problems , but if app is in background didReceiveRemoteNotification doesn't call(I don't see the print "PUSH NOTIFICATION is coming" ) and I don't understand why.
Can you help me?
N.B for testing I use APN Tester(https://itunes.apple.com/it/app/apn-tester-free/id626590577?mt=12) for send push notification
didReceiveRemoteNotification is meant to be used when the app is active.
When the app is in the background or inactive, you can activate it by pressing the action button on the remote notification. Implement userNotificationCenter(_:didReceive:withCompletionHandler:) in your app delegate.
In AppDelegate.swift :
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
// make your function call
}
didReceive mean is: when your Application is Background then you click the notification didReceive is working

Resources