Xamarin.Forms iOS 无法自定义通知

Xamarin.Forms iOS Can't Customize Notifications

我一直在按照位于 https://azure.microsoft.com/en-us/documentation/articles/partner-xamarin-mobile-services-xamarin-forms-get-started-push/ 的教程进行操作,尝试让推送通知在我的 Xamarin.Forms 应用程序中工作。

我让他们在 Android 上工作,但我在 iOS 上有一个错误 - 我需要在通知之前自定义文本(来自 phone)实际上是制作的,但我不能在 ios 上,因为当应用程序在后台 运行 时,不会调用 ReceivedRemoteNotification。

这是类似于我的通知处理代码的样子:

public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
    NSObject inAppMessage;

    bool success = userInfo.TryGetValue(new NSString("inAppMessage"), out inAppMessage);

    if (success)
    {
        //change the text that is displayed
        string newNotText = ModifyNotification(inAppMessage.ToString());

        var alert = new UIAlertView("Got push notification", newNotText, null, "OK", null);
        alert.Show();
    }
}

如何自定义在 iOS 收到的通知?

iOSPush和GCM的工作方式不同,GCM让App处理通知并启动本地通知,iOS不会。

iOS 仅通知您的应用该通知已存在,但对此有解决方法。

在 iOS 上,您可以使用用户看不到的静默通知,但您会收到 ReceivedRemoteNotification 回调

您可以在此处阅读更多内容:https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html

本文档告诉您以下内容:

content-available 属性 值为 1 让远程通知充当“静默”通知。当静默通知到达时,iOS 会在后台唤醒您的应用程序,以便您可以从服务器获取新数据或进行后台信息处理。用户不会被告知静默通知产生的新信息或更改信息,但他们可以在下次打开您的应用程序时了解这些信息。

因此,如果您的通知包含值为 1 的 "content-available",它将是无声的,您可以在此之后开始您自己的本地通知。

请做好准备,这在任何方面都不可靠,如果您不是特殊特权应用程序(如 VOIP),您将无法在 iOS[=13 上以可靠的方式执行您想要的操作=]

后端示例:

只需像您使用的教程中那样更改模板变量:

const string template = "{\"aps\":{\"content-available\":\"1\",\"alert\":\"$(message)\"}}";

因为不够清楚,如果你不想收到任何通知,你不应该为你的通知使用警报或声音属性

const string template = "{\"aps\":{\"content-available\":\"1\",\"someproperty\":\"propertyvalue\"}}";