OGeek|极客世界-中国程序员成长平台

标题: ios - Xamarin.Forms iOS 无法自定义通知 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:50
标题: ios - Xamarin.Forms iOS 无法自定义通知

我一直在学习以下位置的教程:https://azure.microsoft.com/en-us/documentation/articles/partner-xamarin-mobile-services-xamarin-forms-get-started-push/尝试获取推送通知以在我的 Xamarin.Forms 应用程序中工作。

我已经让他们在 Android 上运行,但我在 iOS 上遇到了一个错误 - 我需要在实际发出通知之前自定义文本(来自手机),但我不能在 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 上收到的通知?



Best Answer-推荐答案


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

iOS 仅通知您的应用通知存在,但有一种解决方法。

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

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

本文档告诉您以下内容:

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

因此,如果您的通知包含值为 1 的“内容可用”,它将保持沉默,您可以在此之后启动自己的本地通知。

做好准备,这在任何方面都不可靠,如果您不是特殊特权应用程序(如 VOIP),您将无法在 iOS 上以可靠的方式做您想做的事

后端示例:

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

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

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

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

关于ios - Xamarin.Forms iOS 无法自定义通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33850182/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4