I have implemented chat functionality in my app, as remote notifications are not possible to send from server side, i am using local notification to alert the user of new incoming message. Everything works fine in simulator, i can see notifications are displayed in foreground as well as background mode.
But when i run the app on device, notification are only working in foreground mode. When app is in background, notifications wont show.
After investigating this issue i found that they work fine when app is running in debug mode(i.e. launched from Xcode and cable still attached). But as soon as i disconnect the cable, they wont show anymore.
Below is the code i have tested, both the code produces the same result. (FYI: It's a Xamarin.IOS app)
UILocalNotification localNotification = new UILocalNotification();
localNotification.FireDate = NSDate.Now;
localNotification.TimeZone = NSTimeZone.DefaultTimeZone;
localNotification.AlertBody = message;
localNotification.SoundName = UILocalNotification.DefaultSoundName;
localNotification.UserInfo = dict;
UIApplication.SharedApplication.ScheduleLocalNotification(localNotification);
var content = new UNMutableNotificationContent();
content.Body = body;
content.Sound = UNNotificationSound.Default;
var requestID = "Chat";
var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
But when i run the app on device, notification are only working in foreground mode. When app is in background, notifications wont show.
After investigating this issue i found that they work fine when app is running in debug mode(i.e. launched from Xcode and cable still attached). But as soon as i disconnect the cable, they wont show anymore.
Below is the code i have tested, both the code produces the same result. (FYI: It's a Xamarin.IOS app)
Code Block language code-block
UILocalNotification localNotification = new UILocalNotification();
localNotification.FireDate = NSDate.Now;
localNotification.TimeZone = NSTimeZone.DefaultTimeZone;
localNotification.AlertBody = message;
localNotification.SoundName = UILocalNotification.DefaultSoundName;
localNotification.UserInfo = dict;
UIApplication.SharedApplication.ScheduleLocalNotification(localNotification);
Code Block language code-block
var content = new UNMutableNotificationContent();
content.Body = body;
content.Sound = UNNotificationSound.Default;
Code Block var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
var requestID = "Chat";
var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
Code Block // schedule it UNUserNotificationCenter.Current.AddNotificationRequest(request, (error) => { if (error != null) { Console.WriteLine($"Error: {error.LocalizedDescription ?? ""}"); } else { Console.WriteLine("Scheduled..."); } });