返回

iOS的Firebase云消息推送通知显示在横幅中,但不会触发应用程序委派方法

发布时间:2022-03-01 12:40:23 536
# android

我正在为我的iOS应用程序实施firebase云消息推送通知。目前,当发送通知时,屏幕上会显示一条带有有效负载的横幅,但不会调用我的应用程序委托方法。

具体来说,当收到推送通知并在横幅中显示时,不会调用userNotificationCenter方法。

为什么我的应用程序委派方法没有触发?

class AppDelegate: NSObject, UIApplicationDelegate {
  func application(_ application: UIApplication,
                   didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

    FirebaseApp.configure()

    UNUserNotificationCenter.current().delegate = self
    Messaging.messaging().delegate = self
    
    return true
  }
}

extension AppDelegate: UNUserNotificationCenterDelegate {
  
  /* Invoked when a notification arrives when the app is running in the foreground. */
  func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    willPresent notification: UNNotification,
    withCompletionHandler completionHandler:
    @escaping (UNNotificationPresentationOptions) -> Void
  ) {
    // Get UNNotificationContent which is derived from the JSON payload sent to APNS from the backend.
    let content = notification.request.content
    
    let userInfo = notification.request.content.userInfo
    Messaging.messaging().appDidReceiveMessage(userInfo)
    
    completionHandler([[.banner, .sound, .badge]])
  }

  /* Invoked when notification arrives when the app is running in the background. */
  func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    didReceive response: UNNotificationResponse,
    withCompletionHandler completionHandler: @escaping () -> Void
  ) {
    let userInfo = response.notification.request.content.userInfo
    Messaging.messaging().appDidReceiveMessage(userInfo)
    completionHandler()
  }
  
  /** Method swizzling enabled requirement. */
  func application(
    _ application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
  ) {
    Messaging.messaging().apnsToken = deviceToken
  }
  
  func application(_ application: UIApplication,
  didReceiveRemoteNotification userInfo: [AnyHashable : Any],
     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    Messaging.messaging().appDidReceiveMessage(userInfo)
    completionHandler(.noData)
  }
}

extension AppDelegate: MessagingDelegate {
  func messaging(
    _ messaging: Messaging,
    didReceiveRegistrationToken fcmToken: String?
  ) {
    let tokenDict = ["token": fcmToken ?? ""]
    NotificationCenter.default.post(
      name: Notification.Name("FCMToken"),
      object: nil,
      userInfo: tokenDict
    )
    writeFCMTokenToFirestore(token: fcmToken)
  }
}

在后端,我在节点中使用firebase admin SDK。js发送带有自定义负载的通知,实际上,它是在我的firestore数据库发生更改时触发的。

const admin = require('firebase-admin');

const token = getDeviceToken();

const payload = {
  notification: {
    title: 'test', 
    badge: '1'
  }
}

admin.messaging().sendToDevice(token, payload)
 .then((response) => {
    // Success
 })
 .catch((error) => {
    // Failure
 });
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像