I have a problem to solve with a push message. If I send a push message to Firebase
,I will receive it well and I will see the message well.
And when I click on the push message, the userNotificationCenter
function in the AppDelegate
file is executed and the list of actions in the function is executed.
Can I execute a function without receiving a specific message and displaying a message?
@available(iOS 10, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let data = response.notification.request.content.userInfo
guard
let push = data[AnyHashable("push")] as? String,
let getdata = data[AnyHashable("getdata")] as? String,
let pushdata = data[AnyHashable("pushdata")] as? String
else {
print("it's not good data")
return
}
print(push)
print(getdata)
print(pushdata)
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}
My purpose is to execute the function without showing the push message to the user when sending a specific message (Ex: push = "noShowPush")
from the Firebase.
I was tried "content_available": true
but get log 6.10.0 - [Firebase/Messaging][I-FCM002019] FIRMessaging received data-message, but FIRMessagingDelegate's-messaging:didReceiveMessage: not implemented
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
// Override point for customization after application launch.
//create the notificationCenter
Messaging.messaging().delegate = self
FirebaseApp.configure()
//Register App For Push Notification
// self.registerAppForPushNotificaition()
let center = UNUserNotificationCenter.current()
let inviteCategory = UNNotificationCategory(identifier: "Notification", actions: [], intentIdentifiers: [], options: UNNotificationCategoryOptions.customDismissAction)
let categories = NSSet(objects: inviteCategory)
center.delegate = self
center.setNotificationCategories(categories as! Set<UNNotificationCategory>)
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})
application.registerForRemoteNotifications()
self.updateAppViewUI()
self.window?.makeKeyAndVisible()
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
Messaging.messaging().appDidReceiveMessage(userInfo)
if Auth.auth().canHandleNotification(userInfo) {
completionHandler(UIBackgroundFetchResult.noData)
return
}
completionHandler(UIBackgroundFetchResult.newData)
}
I have it already Messaging.messaging().delegate = self
.
The logs in the Firebase
have warned you to set FirebaseAppDelegateProxyEnabled
to No
, so I added to the Info.list
.
and Firebase
log changed [Firebase/Core][I-COR000003] The default Firebase app has not yet been configured. Add '[FIRApp configure];' ('FirebaseApp.configure()' in Swift) to your application initialization. Read more:
[Firebase/Core][I-COR000003] The default Firebase app has not yet been configured. Add '[FIRApp configure];' ('FirebaseApp.configure()' in Swift) to your application initialization. Read more:
Are the things I'm revising are going right?
How can I this solution?
I solved the problem. Here's the order in which I've worked out:
"content_available": true"
. And don't include the title
and body
.6.10.0 - [Firebase/Messaging][I-FCM002019] FIRMessaging received data-message, but FIRMessagingDelegate's-messaging:didReceiveMessage: not implemented
The logs in the Firebase have warned you to set FirebaseAppDelegateProxyEnabled
to No, so I added to the Info.list
.
and changed the order of execution of the function.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { FirebaseApp.configure() // Let it run first. Messaging.messaging().delegate = self self.window = UIWindow(frame: UIScreen.main.bounds)...
And I installed 'Firebase/Analytics'
on the pod. pod 'Firebase/Analytics'
and inherited the MessagingDelegate
and implemented the Messaging
function.
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { print("fcmToken \(fcmToken)") } func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) { Log.Info("remort \(remoteMessage.appData)") let userInfo = remoteMessage.appData }
This configuration allows you to receive push message data into the messaging
function when a push message is sent. This is a push message with a ' contentAvailable
' value of True
and no title
and body
.
You can use silent push notifications, which contains "content-available": 1
in the payload. For FCM:
"to" : "[token]",
"content_available": true,
"priority": "high",
"data" : {
"key1" : "abc",
"key2" : 123
}
Reference: https://stackoverflow.com/a/43187302/4061501
Read: Downstream HTTP messages (JSON)
OR in userNotificationCenter(_:willPresent:withCompletionHandler:)
, call completionHandler(.none)
But this will not work when app is closed( please comment if there is any method/way ).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.