【问题标题】:Xamarin.Forms iOS Can't Customize NotificationsXamarin.Forms iOS 无法自定义通知
【发布时间】:2016-02-24 07:52:16
【问题描述】:

我一直在按照以下位置的教程进行操作: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 上收到的通知?

【问题讨论】:

    标签: ios background xamarin push-notification xamarin.forms


    【解决方案1】:

    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\"}}";
    

    【讨论】:

    • 感谢您的回复,我一直在做研究,这与我发现的一致 - 如果您能提供代码示例,我们将不胜感激。
    • 可能是在后台收到通知后处理通知的示例。
    • 我更改了后端代码示例的答案,客户端像处理普通通知一样处理后台通知,代码没有改变。
    • 感谢您的帮助,但是当我更改字符串模板时,我仍然会收到通知。
    • 好的,所以我也将属性称为“警报”,而不仅仅是我必须设置内容可用。非常感谢,如果没有你,我可能再过一个星期都想不通
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 2014-11-01
    • 2017-03-10
    相关资源
    最近更新 更多