【问题标题】:DidReceiveRemoteNotification never called in Xamarin.iOSDidReceiveRemoteNotification 从未在 Xamarin.iOS 中调用
【发布时间】:2018-02-26 15:14:08
【问题描述】:

我正在尝试使用 Xamarin 和 Xamarin.Firebase.iOS.CloudMessaging 包为我的 iOS 应用程序实现推送通知。

我已经完成了所有设置和工作,即。当应用程序处于前台时我可以接收通知(包含和不包含“content-available" 标签”)并与它们交互(点击通知等)

但是,当应用程序在后台时,我会收到通知,但不会调用任何回调。据我了解,"didReceiveRemoteNotification" 应在以下情况下调用:

  1. 无论"content-available" 标签是否启用,应用程序都在前台
  2. 如果应用程序在后台或挂起,启用"content-available"标签
  3. 当用户点击通知时

以下是我在 AppDelegate.cs 中实现的功能:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    global::Xamarin.Forms.Forms.Init();
    Firebase.Core.App.Configure();
    FireBaseRegistration();
    return base.FinishedLaunching(app, options);
}


private void FireBaseRegistration()
{
    // Register your app for remote notifications.
    if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
    {
        // iOS 10 or later
        var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
        UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) => {
            Console.WriteLine(granted);
        });

        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.Current.Delegate = this;
    }
    else
    {  
        // iOS 9 or before
        var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
        var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
        UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
    }

    //Register for APNs notifications
    UIApplication.SharedApplication.RegisterForRemoteNotifications();

    Messaging.SharedInstance.Delegate = this;

    var token = InstanceId.SharedInstance.Token;
    Debug.WriteLine(token);

    //////Connect to FCM (Only used for Foreground notifications)
    Messaging.SharedInstance.ShouldEstablishDirectChannel = true;

    // Monitor token generation
    InstanceId.Notifications.ObserveTokenRefresh((sender, e) => {
        // 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.
        token = InstanceId.SharedInstance.Token;
        Console.WriteLine(token);

    });

}


//Register APNs token because method swizzling is de-activated
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    Console.WriteLine("Registered for remote notifications");
    Console.WriteLine(deviceToken.GetBase64EncodedString(NSDataBase64EncodingOptions.None));
    Console.WriteLine(deviceToken);
}



[Export("messaging:didReceiveMessage:")]
public void DidReceiveMessage(Messaging messaging, RemoteMessage remoteMessage)
{
    // Do your magic to handle the notification data
    Console.WriteLine("iOS 11 Foreground");
}


//Shows local notification and is called when user taps notification
[Export("userNotificationCenter:DidReceiveRemoteNotification:withCompletionHandler:")]
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
    Console.WriteLine("Received a notficaiton");
    completionHandler(UIBackgroundFetchResult.NewData);
}

//To receive notifications in foreground on iOS 11 devices.
[Export("userNotificationCenter:willPresent:withCompletionHandler:")]
public void WillPresent(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
    Console.WriteLine("Handling iOS 11 foreground notification");
    completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);
}

////Called when tapping notification
[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
    Console.WriteLine("Handling push notificaiton interaction");
    completionHandler();
}

//Receive data message on iOS 10 devices.
public void ApplicationReceivedRemoteMessage(RemoteMessage remoteMessage)
{
    Console.WriteLine("Handling iOS 10 data message notification");
}   

//// To receive notifications in foreground on iOS 10 devices.
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
    Console.WriteLine("Handling foreground notification");
    completionHandler(UNNotificationPresentationOptions.Alert);
}

我已经在 iOS 10.3.3 和 iOS 11.2 上尝试过这些。以下是两个版本调用的函数:

  1. 当应用程序在前台时
    • 在 iOS 10.3.3 上:WillPresentNotification()(带有和不带有 "content-available" 标签)
    • 在 iOS 11.2 上:WillPresentNotification()(带有和不带有 "content-available" 标签)
  2. 当应用程序在后台时:
    • 在 iOS 10.3.3 上:什么都不应该是 DidReceiveRemoteNotification
    • 在 iOS 11.2 上:什么都不应该是 DidReceiveRemoteNotification

我在 Info.plist 文件中激活了远程通知和后台获取后台模式。

包裹信息:

  • Xamarin.forms:2.5.0.280555
  • Xamarin.Firebase.iOS.CloudMessage:2.0.4.1
  • Xamarin.Firebase.iOS.Core:4.0.13
  • Xamarin.Firebase.iOS.InstanceID:2.0.8

备注:

DidReceiveMessage 函数在 Xamarin 示例中实现,但就我而言,从未调用过。

当用户点击通知时会调用DidReceiveNotificationResponse

WillPresent 也永远不会被调用。

我尝试过通过 firebase API 和直接通过 APNs(使用推送器应用程序)发送通知。

【问题讨论】:

  • 尝试发送纯数据消息(没有文本等,但带有“内容可用”标志)。我不记得确切但也许“只有数据”和“数据+通知”消息可以不同地处理。
  • 并尝试删除您的“导出”属性。它们不是必需的,但可能会导致此错误。
  • 感谢您的提示。发送“仅数据”消息调用DidReceiveMessage 函数。但是,这仅适用于应用程序在前台时。此外,删除“导出”属性时没有任何效果。
  • 将 Messaging.SharedInstance.ShouldEstablishDirectChannel 设置为 false 以禁用 DidReceiveMessage 调用。
  • 总是?当应用程序在前台时我将它设置为 true,当它在后台时设置为 false。

标签: ios firebase push-notification xamarin.ios firebase-cloud-messaging


【解决方案1】:

设置Messaging.SharedInstance.ShouldEstablishDirectChannel=false 并发送纯数据消息。

【讨论】:

  • 在我的 firebase JSON 中,我已将 "content-available": 1 切换为 "content-available": true。在摆弄其他参数的同时,其中一个请求回复指出 1 无效(不知道为什么之前没有说)。那并将Messaging.SharedInstance.ShouldEstablishDirectChannel 设置为false,似乎已经成功了。嵌入的通知不会导致任何问题。
  • 我刚刚通过添加Messaging.SharedInstance.ShouldEstablishDirectChannel=true 对此进行了测试,didReceiveRemoteNotification 按预期调用。这不是有人第一次体验 Magically 返工功能:forums.xamarin.com/discussion/42314/…
  • @PMARSH 我可以确认我遇到了这个 exact 同样的问题。我已将 ShouldEstablishDirectChannel 属性更改为 true(即使它声称已弃用),现在我正在从 Firebase 控制台发送消息。非常感谢这么
猜你喜欢
  • 1970-01-01
  • 2017-03-28
  • 2020-06-30
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2015-11-14
  • 2015-09-30
  • 1970-01-01
相关资源
最近更新 更多