Skip to content

Instantly share code, notes, and snippets.

@Haosvit
Last active July 15, 2016 02:04
Show Gist options
  • Select an option

  • Save Haosvit/3b0c679fe358063b99449fb1a4c7689d to your computer and use it in GitHub Desktop.

Select an option

Save Haosvit/3b0c679fe358063b99449fb1a4c7689d to your computer and use it in GitHub Desktop.
  1. Open Google's configuration tool to create a config file for your app.

Read help file about provising certivicate for Google's configuration tool.

  1. Follow the GCM for iOS document

! Fix GetToken function, change NSDictionary to NSMutableDictionary

void GetToken ()
{
    // Register APNS Token to GCM
    var options = new NSMutableDictionary ();
    options.SetValueForKey (DeviceToken, Constants.RegisterAPNSOption);
    options.SetValueForKey (new NSNumber(true), Constants.APNSServerTypeSandboxOption);

    // Get our token
    InstanceId.SharedInstance.Token (
        GCM_SENDER_ID,
        Constants.ScopeGCM,
        options,
        (token, error) => Log ("GCM Registration ID: " + token));
}

! Fix SendUpstreamMessage,

int messageId = 1;

// We can send upstream messages back to GCM
public void SendUpstreamMessage ()
{            
    var msg = new NSDictionary ();
    msg.SetValueForKey (new NSString ("1234"), new NSString ("userId"));
    msg.SetValueForKey (new NSString ("hello world"), new NSString ("msg"));

    var to = GCM_SENDER_ID + "@gcm.googleapis.com";

    Service.SharedInstance.SendMessage (msg, to, (messageId++).ToString ());
}
  1. Reference Message Sender in the [Walkthrough - Using Remote Notifications in Xamarin.Android] (https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/android/remote_notifications_in_android/) to push notification from server side.

  2. Add these to attributes for push notification on iOS:

  • content_available: true
  • priority: “high”
  1. Subscribe to a topic:
		private void Subscribe(string token, string topic)
		{
		 Google.GoogleCloudMessaging.PubSub.SharedInstance.Subscribe(token, topic, new NSDictionary(), delegate { });
		}

Link to GCM for iOS - Mvvmcross project: https://github.com/Haosvit/Google-Cloud-Service-for-iOS---Xamarin-Mvvmcross.git

{
  "to": "/topics/global",
  "content_available": true,
  "priority": "high",
  "notification": {
    "title": "Push notification demo",
    "body": "From post man"
  },
  "data": {
      "sender" : "Hao Svit"
  }
}

These key-value is compulsory for pushing notification on iOS

  • "content_available": true,
  • "priority": "high"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment