/// <summary> /// Converts the message of this class to GCM Notification /// </summary> /// <returns></returns> public static GcmNotification ConvertToGCMNotification(MobileNotificationMessage message) { // string messageJSON = "{\"alert\":\"Hello World!\",\"badge\":7,\"sound\":\"sound.caf\"}"; //string messageJSON = "{\"alert\":\"" + GetSafeJSonString(message.Alert) // + "\",\"badge\":" + message.Badge.ToString() + // ",\"sound\":\"" + GetSafeJSonString(message.Sound) + "\"}"; //GCMAirMessage msg = new GCMAirMessage(); CordovaGCMMessage msg = new CordovaGCMMessage(); msg.type = message.Type.getFnName(); msg.message = message.Alert; msg.title = message.Title; msg.id = message.ParamsJSON; //msg.icon = ""; msg.msgcnt = 1; //FreshPlanetAirMessage msg = new FreshPlanetAirMessage(); //msg.tickerText = message.Alert; //msg.contentText = message.Alert; //msg.contentTitle = message.Title; string messageJSON = FWUtils.EntityUtils.JsonSerializeObject(msg); //StringBuilder messageJSON = new StringBuilder(); //messageJSON.Append("{\"alert\":\"" + GetSafeJSonString(message.Alert) + "\""); //messageJSON.Append(",\"type\":\"" + message.Type.getFnName() + "\""); //if (string.IsNullOrEmpty(message.Title) == false) // messageJSON.Append(",\"title\": \"" + GetSafeJSonString(message.Title) + "\""); //if (message.ParamsJSON != null) // messageJSON.Append(",\"id\":\"" + GetSafeJSonString(message.ParamsJSON) + "\""); //messageJSON.Append("}"); GcmNotification gcmMessage = new GcmNotification(); if (message.IsOneSufficient) { gcmMessage.CollapseKey = message.CollapseKey.getFnName(); } gcmMessage.TimeToLive = message.TimeToLiveSeconds; gcmMessage.JsonData = messageJSON; gcmMessage.RegistrationIds.Add(message.DevicePushToken); //gcmMessage.QueuedCount = message.Badge; QueuedCount is for number of messages to send one by one. It is 7, it delivers the message 7 times. I don't know the real usage. It maybe to make sure that message will be delivered gcmMessage.DelayWhileIdle = message.DelayWhileIdle; return(gcmMessage); //return new GcmNotification().ForDeviceRegistrationId(this.DevicePushToken) // .WithJson(messageJSON); }
/// <summary> /// Converts the message of this class to Apple Notification /// </summary> /// <returns></returns> public static AppleNotification ConvertToAppleNotification(MobileNotificationMessage message) { AppleNotification appleMessage = new AppleNotification(); appleMessage.DeviceToken = message.DevicePushToken; if (message.TimeToLiveSeconds.HasValue) { if (message.TimeToLiveSeconds.Value != 0) // 0 means instant sending in GCM, but here it just expires the message { appleMessage.Expiration = DateTime.UtcNow.AddSeconds(message.TimeToLiveSeconds.Value); } } appleMessage.Payload = new AppleNotificationPayload(message.Alert, message.Badge, message.Sound); return(appleMessage); //return new AppleNotification() // .ForDeviceToken(this.DevicePushToken) // .WithAlert(this.MessageBody) // .WithBadge(this.Badge) // .WithSound(this.Sound); }
/// <summary> /// Sends a push notification to Google Cloud Messaging /// /// GCM will store up to 100 non-collapsible messages. /// Order of delivery is not guaranteed /// Read more here: http://developer.android.com/google/gcm/adv.html#retry /// </summary> /// <param name="message">the message object</param> public static void SendGoogleNotification(MobileNotificationMessage message) { //Fluent construction of an Android GCM Notification //IMPORTANT: For Android you MUST use your own RegistrationId here that gets generated within your Android app itself! push.QueueNotification(ConvertToGCMNotification(message)); }
/// <summary> /// Sends a push notification to Apple Push Notification Service /// </summary> /// <param name="message">the message object</param> public static void SendAppleNotification(MobileNotificationMessage message) { push.QueueNotification(ConvertToAppleNotification(message)); }