简体   繁体   中英

How to handle firebase cloud message notification in foreground in standalone Watch OS app?

I am trying to listen to firebase cloud message notifications on standalone Apple Watch app. In Flutter framework there is

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    //handle messsage as necessary
});

I have found out https://firebase.google.com/docs/cloud-messaging/ios/receive#handle_silent_push_notifications to send silent notification but when I am trying to perform the same, I am not getting any notification, from fcm portal on firebase and postman

The code I am using:

import SwiftUI
import Firebase

@main
struct Curiousfly_Watch_Watch_AppApp: App {
  @WKApplicationDelegateAdaptor(FCMWatchAppDelegate.self) var appDelegate
  var body: some Scene {
    WindowGroup {
      AuthenticationView()
    }
  }
}

// MARK: - WKApplicationDelegate
class FCMWatchAppDelegate: NSObject, WKApplicationDelegate, MessagingDelegate {
  @AppStorage("fcmToken") var fToken = ""
  func applicationDidFinishLaunching() {
    FirebaseApp.configure()
    let notificationCenter = UNUserNotificationCenter.current()
    notificationCenter.requestAuthorization(options: [.alert, .sound]) { granted, error in
      if granted {
        WKApplication.shared().registerForRemoteNotifications()
      }
    }
    Messaging.messaging().delegate = self
  }

  func didRegisterForRemoteNotifications(withDeviceToken deviceToken: Data) {
    print("APNS didRegisterForRemoteNotifications. Got device token \(deviceToken)")
    Messaging.messaging().apnsToken = deviceToken
  }
    
  func application(_ application: Messaging,
   didReceiveRemoteNotification userInfo: [AnyHashable: Any]) async
   -> WKBackgroundFetchResult {
    if let messageID = userInfo["gcm.message_id"] {
      print("Message ID: \(messageID)")
    }
    print(userInfo)
    return WKBackgroundFetchResult.newData
  }
}

// MARK: - FCM MessagingDelegate
extension FCMWatchAppDelegate {
  func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
    // Use this FCM token to test sending a push using API or Firebase Console
    print("FCM - didReceiveRegistrationToken \(String(describing: fcmToken))")
    fToken = fcmToken!
  }
}

Terminal output:

2022-12-28 17:38:25.857753+0530 Curiousfly Watch Watch App[384:120474] [SceneConfiguration] Info.plist contained no UIScene configuration dictionary (looking for configuration named "(no name)")
2022-12-28 17:38:25.858911+0530 Curiousfly Watch Watch App[384:120474] [SceneConfiguration] Info.plist contained no UIScene configuration dictionary (looking for configuration named "(no name)")
2022-12-28 17:38:25.859063+0530 Curiousfly Watch Watch App[384:120474] [SceneConfiguration] Info.plist contained no UIScene configuration dictionary (looking for configuration named "Default Configuration")

FCM - didReceiveRegistrationToken Optional("dYJGIGsXqEN0p7syDPCxU1:APA91bEFMfqb5C5o9yKgNwq7wOoX22olo1oRm9duPBEC6exfZwadb_7AF5GFaJ6aYV4KWebHhdTXwxDYKzxa7SC-DLUQoNkx15U5gS2POjf6oN_f0gVyabQwbfk3WPy_je3yejeZxjZh")
2022-12-28 17:38:26.036943+0530 Curiousfly Watch Watch App[384:120474] void * _Nullable NSMapGet(NSMapTable * _Nonnull, const void * _Nullable): map table argument is NULL
2022-12-28 17:38:26.067010+0530 Curiousfly Watch Watch App[384:120688] 10.3.0 - [FirebaseMessaging][I-FCM002022] APNS device token not set before retrieving FCM Token for Sender ID '984386421852'. Notifications to this FCM Token will not be delivered over APNS.Be sure to re-retrieve the FCM token once the APNS device token is set.
Dec 28 17:38:26  Curiousfly Watch Watch App[384] <Warning>: 10.3.0 - [FirebaseMessaging][I-FCM002022] APNS device token not set before retrieving FCM Token for Sender ID '984386421852'. Notifications to this FCM Token will not be delivered over APNS.Be sure to re-retrieve the FCM token once the APNS device token is set.
2022-12-28 17:40:18.138006+0530 Curiousfly Watch Watch App[384:121080] Task <136B1980-5B70-4907-B184-B55CF2D1F9D7>.<1> finished with error [-1001] Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSLocalizedDescription=The request timed out., _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <136B1980-5B70-4907-B184-B55CF2D1F9D7>.<1>, _kCFStreamErrorCodeKey=-2103, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <136B1980-5B70-4907-B184-B55CF2D1F9D7>.<1>"
), NSErrorFailingURLStringKey=https://device-provisioning.googleapis.com/checkin, _kCFStreamErrorDomainKey=4, NSErrorFailingURLKey=https://device-provisioning.googleapis.com/checkin}
2022-12-28 17:41:50.385390+0530 Curiousfly Watch Watch App[384:120474] [bg_app_refresh] -[SPApplicationDelegate prepareForSnapshotWithReason:userInfo:completion:]:3099: PrepareForSnapshot called on an application that is running in the dock or foreground, completing immediately
2022-12-28 17:41:55.624735+0530 Curiousfly Watch Watch App[384:120683] [AXRuntimeCommon] Unknown client: Curiousfly Watch Watch App

2022-12-28 17:42:24.963294+0530 Curiousfly Watch Watch App[384:121587] [AXRuntimeCommon] AX Lookup problem - errorCode:1100 error:Permission denied portName:'com.apple.iphone.axserver' PID:227 (
    0   AXRuntime                           0x3aabc450 _AXGetPortFromCache + 1016
    1   AXRuntime                           0x3aabeac4 AXUIElementPerformFencedActionWithValue + 940
    2   UIKit                               0x03d3dc64 __64-[_UIRemoteViewAccessibility _accessibilityUnregisterRemoteView]_block_invoke_2 + 60
    3   libdispatch.dylib                   0x0357b848 _dispatch_call_block_and_release + 24
    4   libdispatch.dylib                   0x0357d040 _dispatch_client_callout + 16
    5   libdispatch.dylib                   0x03584168 _dispatch_lane_serial_drain + 660
    6   libdispatch.dylib                   0x03584dfc _dispatch_lane_invoke + 436
    7   libdispatch.dylib                   0x0359077c _dispatch_workloop_worker_thread + 712
    8   libsystem_pthread.dylib             0x7fb9d47c _pthread_wqthread + 280
    9   libsystem_pthread.dylib             0x7fb9d51c start_wqthread + 8
)
2022-12-28 17:42:24.965915+0530 Curiousfly Watch Watch App[384:121587] [AXRuntimeCommon] AX Lookup problem - errorCode:1100 error:Permission denied portName:'com.apple.iphone.axserver' PID:227 (
    0   AXRuntime                           0x3aabc450 _AXGetPortFromCache + 1016
    1   AXRuntime                           0x3aabeac4 AXUIElementPerformFencedActionWithValue + 940
    2   UIKit                               0x03d3dc64 __64-[_UIRemoteViewAccessibility _accessibilityUnregisterRemoteView]_block_invoke_2 + 60
    3   libdispatch.dylib                   0x0357b848 _dispatch_call_block_and_release + 24
    4   libdispatch.dylib                   0x0357d040 _dispatch_client_callout + 16
    5   libdispatch.dylib                   0x03584168 _dispatch_lane_serial_drain + 660
    6   libdispatch.dylib                   0x03584dfc _dispatch_lane_invoke + 436
    7   libdispatch.dylib                   0x0359077c _dispatch_workloop_worker_thread + 712
    8   libsystem_pthread.dylib             0x7fb9d47c _pthread_wqthread + 280
    9   libsystem_pthread.dylib             0x7fb9d51c start_wqthread + 8
)

2022-12-28 17:42:38.041972+0530 Curiousfly Watch Watch App[384:121552] PDTask <ED5955B6-BACE-457C-8B32-43C101C4EDA5>.<2> finished with error [9] Error Domain=NSPOSIXErrorDomain Code=9 "Bad file descriptor" UserInfo={_kCFStreamErrorCodeKey=9, _kCFStreamErrorDomainKey=1, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataPDTask <ED5955B6-BACE-457C-8B32-43C101C4EDA5>.<2>",
    "LocalDataTask <ED5955B6-BACE-457C-8B32-43C101C4EDA5>.<2>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataPDTask <ED5955B6-BACE-457C-8B32-43C101C4EDA5>.<2>}
2022-12-28 17:42:38.042704+0530 Curiousfly Watch Watch App[384:121552] Task <ED5955B6-BACE-457C-8B32-43C101C4EDA5>.<3> finished with error [9] Error Domain=NSPOSIXErrorDomain Code=9 "Bad file descriptor" UserInfo={_kCFStreamErrorCodeKey=9, _kCFStreamErrorDomainKey=1, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <ED5955B6-BACE-457C-8B32-43C101C4EDA5>.<3>",
    "LocalDataPDTask <ED5955B6-BACE-457C-8B32-43C101C4EDA5>.<2>",
    "LocalDataTask <ED5955B6-BACE-457C-8B32-43C101C4EDA5>.<2>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <ED5955B6-BACE-457C-8B32-43C101C4EDA5>.<3>}

Forgot to add

func didReceiveRemoteNotification(_ userInfo: [AnyHashable : Any]) async -> WKBackgroundFetchResult {
    NSLog("\(userInfo)")
    return WKBackgroundFetchResult.newData
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM