简体   繁体   中英

Xamarin.Forms iOS Can't Customize Notifications

I've been following the tutorial found at: https://azure.microsoft.com/en-us/documentation/articles/partner-xamarin-mobile-services-xamarin-forms-get-started-push/ to try and get push notifications to work in my Xamarin.Forms app.

I've got them working on Android, but I'm having a bug on iOS - I need to customize the text (from the phone) before the notification is actually made, but I can't on ios since when the app is running in the background, ReceivedRemoteNotification isn't being called.

Here's something similar to what my notification handling code looks like:

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();
    }
}

How can I customize a notification that is received on iOS?

iOS Push and GCM is different in how they work, GCM lets the App handle the notification and start the local notification, iOS does not.

iOS only notifies your App that the notification existed, but there are a workarounds for this.

On iOS you can use silent notifications which are not seen by the user but you'll get the ReceivedRemoteNotification callback

You can read more here: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html

This documentation tells you the following:

The content-available property with a value of 1 lets the remote notification act as a “silent” notification. When a silent notification arrives, iOS wakes up your app in the background so that you can get new data from your server or do background information processing. Users aren't told about the new or changed information that results from a silent notification, but they can find out about it the next time they open your app.

So if your notification contains the "content-available" with value 1 it will be silent and you can start your own local notification after this.

Be prepared that this is not reliable in any way and if you are not a special privileged app(like VOIP) you are not able to do what you want in a reliable way on iOS

Backend example:

Just change the template var like in your tutorial used:

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

Because it is not clear enough, if you don't want to get any notification you should not use alert or sound properties for your notification

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM