private void OnPushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args) { NotificationData notificationData = null; try { //Decide what to do when a notification arrives while the app is running switch (args.NotificationType) { case PushNotificationType.Toast: var toastNotification = JsonConvert.DeserializeObject<IToastText02>(args.ToastNotification.Content.GetXml()); notificationData = new NotificationData { AlertMessage = String.Concat(toastNotification.TextHeading.Text, " ", toastNotification.TextBodyWrap.Text), CustomDataJsonString = toastNotification.Launch, AppWasRunning = true }; break; case PushNotificationType.Tile: break; case PushNotificationType.Badge: break; case PushNotificationType.Raw: break; } } catch (Exception e) { WindowsPhoneUtils.Log(String.Concat("Error on PushNotification received: ", e.Message)); } WindowsPhoneUtils.InvokeCallback("Appverse.PushNotifications.OnRemoteNotificationReceived", WindowsPhoneUtils.CALLBACKID, JsonConvert.SerializeObject(notificationData)); //Show Push notification even when app is in foreground args.Cancel = false; }
public abstract void ScheduleLocalNotification(NotificationData notification, SchedulingData schedule);
public abstract void PresentLocalNotificationNow(NotificationData notification);
private void ProcessRemoteNotification(NSDictionary options, bool fromFinishedLaunching, UIApplicationState applicationState) { //Check to see if the dictionary has the aps key. This is the notification payload you would have sent if (options!=null && options.ContainsKey(new NSString("aps"))) { #if DEBUG log (" ******* PROCESSING REMOTE NOTIFICATION Notification Payload received"); #endif NotificationData notificationData = new NotificationData (); string alert = string.Empty; string sound = string.Empty; int badge = -1; try { //Get the aps dictionary NSDictionary aps = options.ObjectForKey (new NSString ("aps")) as NSDictionary; //Extract the alert text //NOTE: Just for the simple alert specified by " aps:{alert:"alert msg here"} " // For complex alert with Localization keys, etc., the "alert" object from the aps dictionary // will be another NSDictionary... Basically the json gets dumped right into a NSDictionary, so keep that in mind if (aps.ContainsKey (new NSString ("alert"))) { string alertType = "undefined"; if (aps[new NSString ("alert")].GetType () == typeof(NSString)) { alert = (aps [new NSString ("alert")] as NSString).ToString (); alertType = "NSString"; } else if (aps [new NSString ("alert")].GetType () == typeof(NSDictionary)) { NSDictionary alertNSDictionary = aps.ObjectForKey (new NSString ("alert")) as NSDictionary; alertType = "NSDictionary"; // We only get "body" key from that dictionary if (alertNSDictionary.ContainsKey (new NSString ("body")) && (alertNSDictionary[new NSString ("body")].GetType () == typeof(NSString))) { alert = (alertNSDictionary [new NSString ("body")] as NSString).ToString (); } } #if DEBUG log ("******* PROCESSING NOTIFICATION Notification Payload contains an alert message. Type [" + alertType + "]"); #endif } //Extract the sound string if (aps.ContainsKey (new NSString ("sound")) && (aps [new NSString ("sound")].GetType() == typeof(NSString))) { sound = (aps [new NSString ("sound")] as NSString).ToString (); #if DEBUG log ("******* PROCESSING NOTIFICATION Notification Payload contains sound"); #endif } //Extract the badge if (aps.ContainsKey (new NSString ("badge")) && (aps [new NSString ("badge")].GetType() == typeof(NSObject))) { string badgeStr = (aps [new NSString ("badge")] as NSObject).ToString (); int.TryParse (badgeStr, out badge); #if DEBUG log ("******* PROCESSING NOTIFICATION Notification Payload contains a badge number: " + badge); #endif } //If this came from the ReceivedRemoteNotification while the app was running, // we of course need to manually process things like the sound, badge, and alert. if (!fromFinishedLaunching && applicationState == UIApplicationState.Active) { #if DEBUG log ("******* PROCESSING NOTIFICATION app was running, so manually showing notification"); #endif UIRemoteNotificationType enabledRemoteNotificationTypes = UIApplication.SharedApplication.EnabledRemoteNotificationTypes; bool alertEnabled = ((enabledRemoteNotificationTypes & UIRemoteNotificationType.Alert) == UIRemoteNotificationType.Alert); bool soundEnabled = ((enabledRemoteNotificationTypes & UIRemoteNotificationType.Sound) == UIRemoteNotificationType.Sound); bool badgeEnabled = ((enabledRemoteNotificationTypes & UIRemoteNotificationType.Badge) == UIRemoteNotificationType.Badge); #if DEBUG log ("******* PROCESSING NOTIFICATION types enabled: alert[" + alertEnabled+"], sound[" + soundEnabled + "], badge[" + badgeEnabled+ "]"); #endif //Manually set the badge in case this came from a remote notification sent while the app was open if (badgeEnabled) { this.UpdateApplicationIconBadgeNumber (badge); } //Manually play the sound if (soundEnabled) { this.PlayNotificationSound (sound); } //Manually show an alert if (alertEnabled) { this.ShowNotificationAlert ("Notification", alert); } } Dictionary<String,Object> customDic = IPhoneUtils.GetInstance ().ConvertToDictionary (new NSMutableDictionary (options)); customDic.Remove ("aps"); // it is not needed to pass the "aps" (notification iOS data) inside the "custom data json string" notificationData.CustomDataJsonString = IPhoneUtils.GetInstance ().JSONSerialize (customDic); } catch (System.Exception ex) { #if DEBUG log (" ******* Unhanlded exception processing notification payload received. Exception message: " + ex.Message); #endif } finally { notificationData.AlertMessage = alert; notificationData.Badge = badge; notificationData.Sound = sound; IPhoneUtils.GetInstance ().FireUnityJavascriptEvent ("Unity.OnRemoteNotificationReceived", notificationData); } } else { #if DEBUG log (" ******* NO Notification Payload received"); #endif } }
/// <summary> /// Notifies the javascript layer when the application has been launched using a notification /// </summary> /// <param name="ev">The LaunchActivatedEventArgs received on application start</param> /// <returns></returns> public async Task ProcessPushNotification(LaunchActivatedEventArgs ev) { NotificationData notificationData = new NotificationData { AlertMessage = String.Concat(ev.Arguments), CustomDataJsonString = ev.Arguments, AppWasRunning = ev.PreviousExecutionState == ApplicationExecutionState.Running }; await WindowsPhoneUtils.InvokeCallback("Appverse.PushNotifications.OnRemoteNotificationReceived", "callbackid", JsonConvert.SerializeObject(notificationData)); }
/// <summary> /// Processes the local notification. /// </summary> /// <param name="application">Application.</param> /// <param name="localNotification">Local notification.</param> private void ProcessLocalNotification(UIApplicationState applicationState, UILocalNotification localNotification) { if(localNotification != null) { #if DEBUG log ("******* Local NOTIFICATION received"); #endif if (applicationState == UIApplicationState.Active) { // we need to manually process the notification while application is running. #if DEBUG log ("******* Application is running, manually showing notification"); #endif this.UpdateApplicationIconBadgeNumber(localNotification.ApplicationIconBadgeNumber); this.PlayNotificationSound(localNotification.SoundName); this.ShowNotificationAlert("Notification", localNotification.AlertBody); } NotificationData notificationData = new NotificationData(); notificationData.AlertMessage = localNotification.AlertBody; notificationData.Badge = localNotification.ApplicationIconBadgeNumber; notificationData.Sound = localNotification.SoundName; if(localNotification.UserInfo != null) { Dictionary<String,Object> customDic = IPhoneUtils.GetInstance().ConvertToDictionary(new NSMutableDictionary(localNotification.UserInfo)); notificationData.CustomDataJsonString = IPhoneUtils.GetInstance().JSONSerialize(customDic); } IPhoneUtils.GetInstance().FireUnityJavascriptEvent("Unity.OnLocalNotificationReceived", notificationData); } }
public override void ScheduleLocalNotification (NotificationData notification, SchedulingData schedule) { UIApplication.SharedApplication.InvokeOnMainThread (delegate { if(notification!=null) { UILocalNotification localNotification = this.PrepareLocalNotification(notification); if(schedule != null) { localNotification.FireDate = IPhoneUtils.DateTimeToNSDate(DateTime.SpecifyKind(schedule.FireDate, DateTimeKind.Local)); SystemLogger.Log(SystemLogger.Module.PLATFORM,"Scheduling local notification at " + schedule.FireDate.ToLongTimeString() + ", with a repeat interval of: " + schedule.RepeatInterval); NSCalendarUnit repeatInterval = 0; // The default value is 0, which means don't repeat. if(schedule.RepeatInterval.Equals(RepeatInterval.HOURLY)) { repeatInterval = NSCalendarUnit.Hour; } else if(schedule.RepeatInterval.Equals(RepeatInterval.DAILY)) { repeatInterval = NSCalendarUnit.Day; } else if(schedule.RepeatInterval.Equals(RepeatInterval.WEEKLY)) { repeatInterval = NSCalendarUnit.Week; } else if(schedule.RepeatInterval.Equals(RepeatInterval.MONTHLY)) { repeatInterval = NSCalendarUnit.Month; } else if(schedule.RepeatInterval.Equals(RepeatInterval.YEARLY)) { repeatInterval = NSCalendarUnit.Year; } localNotification.RepeatInterval = repeatInterval; UIApplication.SharedApplication.ScheduleLocalNotification(localNotification); SystemLogger.Log(SystemLogger.Module.PLATFORM, "Local Notification scheduled successfully [" + localNotification.FireDate.ToString() +"]"); SystemLogger.Log(SystemLogger.Module.PLATFORM, "Current scheduled #num of local notifications: " + this.GetCurrentScheduledLocalNotifications()); } else { SystemLogger.Log(SystemLogger.Module.PLATFORM,"No suitable scheduling data object received for scheduling this local notification"); } } else { SystemLogger.Log(SystemLogger.Module.PLATFORM,"No suitable data object received for presenting local notification"); } }); }
public override void PresentLocalNotificationNow (NotificationData notification) { UIApplication.SharedApplication.InvokeOnMainThread (delegate { if(notification!=null) { UILocalNotification localNotification = this.PrepareLocalNotification(notification); UIApplication.SharedApplication.PresentLocalNotificationNow(localNotification); } else { SystemLogger.Log(SystemLogger.Module.PLATFORM,"No suitable data object received for presenting local notification"); } }); }
private UILocalNotification PrepareLocalNotification (NotificationData notification) { if(notification!=null) { notification.AppWasRunning = true; UILocalNotification localNotification = new UILocalNotification(); localNotification.AlertBody = notification.AlertMessage; localNotification.ApplicationIconBadgeNumber = notification.Badge; localNotification.SoundName = UILocalNotification.DefaultSoundName; // defaults if(notification.Sound != null && notification.Sound.Length>0 && !notification.Sound.Equals("default")) { // for sounds different from the default one localNotification.SoundName = notification.Sound; } if(notification.CustomDataJsonString != null && notification.CustomDataJsonString.Length>0) { SystemLogger.Log(SystemLogger.Module.PLATFORM,"Custom Json String received: " + notification.CustomDataJsonString); Dictionary<String,Object> userDictionary = (Dictionary<String,Object>) IPhoneUtils.GetInstance().JSONDeserialize<Dictionary<String,Object>>(notification.CustomDataJsonString); localNotification.UserInfo = IPhoneUtils.GetInstance().ConvertToNSDictionary(userDictionary); } return localNotification; } else { return null; } }
public override void ScheduleLocalNotification(NotificationData notification, SchedulingData schedule) { throw new NotImplementedException(); }
public override void PresentLocalNotificationNow(NotificationData notification) { throw new NotImplementedException(); }