菜鸟教程小白 发表于 2022-12-11 18:12:09

ios - Firebase 消息传递方法 swizzling 不起作用


                                            <p><p>您好,我正在实现 FCM(Fire base 云消息传递)。我已经让应用程序从 FCM 控制台接收通知和数据。我有我的服务器通知和 Firebase 通知。我想分别处理两种类型的通知(来 self 的服务器和来自 FCM)。我已经浏览了文档,这可以通过在 AppDelegate 中调整通知处理程序来实现</p>

<p>但问题是我只能在</p>中获取FCM消息数据(IOS 10)

<pre><code>-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {}
</code></pre>

<p>和</p>

<pre><code>-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {}
</code></pre>

<p>FCM 委托(delegate)的回调</p>

<pre><code>// The callback to handle data message received via FCM for devices running iOS 10 or above.
- (void)applicationReceivedRemoteMessage:(nonnull FIRMessagingRemoteMessage *)remoteMessage {

    // Print full message
    NSLog(@&#34;Can get here firebase?%@&#34;, );
}
</code></pre>

<p>上面的回调永远不会被调用</p>

<p>我们如何使用 swizzling 单独设置 FCM 处理程序消息?</p>

<p>这是我的 Appdelegate didfinishlaunchingwith 选项</p>

<pre><code>    application.applicationIconBadgeNumber = 0;

    if (floor(NSFoundationVersionNumber) &lt;= NSFoundationVersionNumber_iOS_9_x_Max) {
      UIUserNotificationType allNotificationTypes =
      (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
      UIUserNotificationSettings *settings =
      ;
      [ registerUserNotificationSettings:settings];
    } else {
      // iOS 10 or later
#if defined(__IPHONE_10_0) &amp;&amp; __IPHONE_OS_VERSION_MAX_ALLOWED &gt;= __IPHONE_10_0
      UNAuthorizationOptions authOptions =
      UNAuthorizationOptionAlert
      | UNAuthorizationOptionSound
      | UNAuthorizationOptionBadge;
      [
         requestAuthorizationWithOptions:authOptions
         completionHandler:^(BOOL granted, NSError * _Nullable error) {
         }
         ];

      // For iOS 10 display notification (sent via APNS)
      [ setDelegate:self];
      // For iOS 10 data message (sent via FCM)

      [ setRemoteMessageDelegate:self];
#endif
    }

    [ registerForRemoteNotifications];

    //
    ;
    //
    // Add observer for InstanceID token refresh callback.
    [ addObserver:self selector:@selector(tokenRefreshNotification:)
                                                 name:kFIRInstanceIDTokenRefreshNotification object:nil];   

    self.window = [ initWithFrame:[ bounds]];
    self.window.backgroundColor = ;

    _rootViewController = [ initWithNibName:nil bundle:nil];


    ;
    ;

    NSDictionary *remoteNotification = ;
    if (remoteNotification)
      ;

    self.isFirstTime = true;
    self.isJustForRefresh = 0;

    return YES;
</code></pre>

<p>有人知道如何让它工作吗?</p>

<p>编辑:这是我的观察者观察函数和didregisterRemoteNotification函数</p>

<pre><code>//
- (void)tokenRefreshNotification:(NSNotification *)notification {
    // Note that this callback will be fired everytime a new token is generated, including the first
    // time. So if you need to retrieve the token as soon as it is available this is where that
    // should be done.
    NSString *refreshedToken = [ token];
    NSLog(@&#34;InstanceID token: %@&#34;, refreshedToken);

    // Connect to FCM since connection may have failed when attempted before having a token.
    ;

    // TODO: If necessary send token to application server.
}
//

//
- (void)connectToFcm {
    [ connectWithCompletion:^(NSError * _Nullable error) {
      if (error != nil) {
            NSLog(@&#34;Unable to connect to FCM. %@&#34;, error);
      } else {
            NSLog(@&#34;Connected to FCM.&#34;);
      }
    }];
}
//

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
    NSString *sanitizedDeviceToken = [[[
                                        stringByReplacingOccurrencesOfString:@&#34;&lt;&#34; withString:@&#34;&#34;]
                                       stringByReplacingOccurrencesOfString:@&#34;&gt;&#34; withString:@&#34;&#34;]
                                    stringByReplacingOccurrencesOfString:@&#34; &#34; withString:@&#34;&#34;];

    DLog(@&#34;sanitized device token: %@&#34;, sanitizedDeviceToken);
    [PushNotificationManager API_registerAPNSToken:sanitizedDeviceToken
                                    onCompletion:^(BOOL success, NSError *error){}];

    [setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];

}
</code></pre>

<p>更新:我已包含此功能但仍无法正常工作</p>

<pre><code>-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    if (userInfo[@&#34;gcm.message_id&#34;] != nil) {
      NSLog(@&#34;Should get here FCM?&#34;);
      [appDidReceiveMessage:userInfo];
    }else {
      NSLog(@&#34;test fetchCompletion handler: %@&#34;,userInfo.description);
    }

}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你可以看看这个答案吗?好像你错过了 <code>FIRMessaging.messaging().connect(){....</code></p> 行

<p> <a href="https://stackoverflow.com/a/40881754/1005570" rel="noreferrer noopener nofollow">https://stackoverflow.com/a/40881754/1005570</a> </p>

<p>还有委托(delegate)函数..</p>

<pre><code>func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: ,
             fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -&gt; Void) {
// 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

// Print message ID.
print(&#34;Message ID: \(userInfo[&#34;gcm.message_id&#34;]!)&#34;)

// Print full message.
print(userInfo)

FIRMessaging.messaging().appDidReceiveMessage(userInfo)
</code></pre>

<p>}</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Firebase 消息传递方法 swizzling 不起作用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40901942/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40901942/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Firebase 消息传递方法 swizzling 不起作用