FirebasePushNotificationManager.Initialize freeze my app in iOS but no error appears - firebase

I am using Xamarin with C#.
FirebasePushNotificationManager.Initialize(options, true);
^ this code works fine in my Android Application.cs file, I can receive notifications in Android. However, when I add this code to AppDelegate.cs in iOS, it freezes my app, meaning there's no response when I click on button or input field. But if I remove this line of code, my iOS app works.
I've Googled a lot but didn't get any luck, can someone please help me?
Following is my AppDelegate.cs code:
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
ServicePointManager
.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
global::Xamarin.Forms.Forms.Init();
var iosClientHandler = new NSUrlSessionHandler();
Services.HttpClientService.HttpClientHandler = iosClientHandler;
LoadApplication(new App());
//This line is causing the issue
FirebasePushNotificationManager.Initialize(options, true);
return base.FinishedLaunching(app, options);
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken);
}
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
FirebasePushNotificationManager.RemoteNotificationRegistrationFailed(error);
}
// To receive notifications in foreground on iOS 9 and below
// To receive notifications in background in any iOS version
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired 'till the user taps on the notification launching the application.
// If you disable method swizzling, you'll need to call this method.
// This lets FCM track message delivery and analytics, which is performed
// automatically with method swizzling enabled
FirebasePushNotificationManager.DidReceiveMessage(userInfo);
// Do your magic to handle the notification data
System.Console.WriteLine(userInfo);
completionHandler(UIBackgroundFetchResult.NewData);
}
}

Related

How to pass a parameter from MainActivity to App.cs in method Resume

I have no idea how to pass a parameter from MainActivity.cs
this code from App.cs
protected override void OnResume(int notificationId)
{
MainPage = new AppShell(notificationId);
}
I plan to pass a number in the "notificationId" parameter. To find a row in the database by "notificationId".
If you're integrating push notification(e,g Azure notification hubs) into your application , OnResume is not the appropriate method to handle this .
Actually every push notification kit has its own trigger method when receiving notification (or tapping on the notification alert) .
For example , we can handle the notification in method OnPushNotificationReceived with Azure notification .
// Set the delegate for receiving messages
NotificationHub.SetListener(new SampleNotificationListener());
// The notification listener implementation
public class SampleNotificationListener : Java.Lang.Object, INotificationListener
{
public void OnPushNotificationReceived(Context context, INotificationMessage message)
{
Console.WriteLine($"Message received with title {message.Title} and body {message.Body}");
}
}
Refer to https://github.com/Azure/azure-notificationhubs-xamarin#getting-started-with-xamarinandroid.
Then we can use Messaging Center to send the data to Forms project if you want .
Something like
//Android
public void OnPushNotificationReceived(Context context, INotificationMessage message)
{
MessagingCenter.Send<object,int >(this, "Hi",notificationId);
}
//Forms App.cs
MessagingCenter.Subscribe<object,int> (this, "Hi", (sender,id) =>
{
MainPage = new AppShell(id);
});

Xamarin forms IOS App aksing for RootviewController

I am working on Xamarin Forms on VS 2017. Whenever I am running My IOS app it is asking for RootViewController but my other sample app is working perfectly without any exception.
My Other app has a default Implementation of Finishedlaunching. AppDelegate is inherited by FormsApplicationDelegate class
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
My main app works only after I have updated the Finishedlaunching Method. AppDelegate is inherited by UIApplicationDelegate class
UIWindow window;
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
Rg.Plugins.Popup.Popup.Init();
window = new UIWindow(UIScreen.MainScreen.Bounds);
window.RootViewController = new Login().CreateViewController();
window.MakeKeyAndVisible();
return true;
}
My question is when my other app is working fine with default code why my second application is not working and I have to add RootViewController.
Please let me know if I am missing anything.

xamarin.forms detect ios app entering foreground from background

I am creating app in Xamarin.Forms shared proeject. I want to detect app when it comes from background
currently, I have implemented OnActivated method in AppDelegate but this method is also called when we open control center in iphone. I want my app to reconnect with my server when app comes to foreground from background and show message accordingly but this message is also shown when I swipe up and open control center.
In native Xamarin.iOS app, there is a method WillEnterForegroundNotification but this method is not available here.
Is there any method that's called only if app comes from background ?
in App.xaml.cs
protected override void OnResume()
In native Xamarin.iOS app, there is a method
WillEnterForegroundNotification but this method is not available here.
You can access WillEnterForegroundNotification method in Xamarin.forms project, just override it in AppDelegate.cs:
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
public override void WillEnterForeground(UIApplication uiApplication)
{
//handle your event here
base.WillEnterForeground(uiApplication);
}
}

xamarin.ios receive notification when app is in background

How can I handle notifications if my App in in Background and incative ?
Now, I get an Alert, which opens my App, when I tap on it.
But when the Notification receives I want to handle the ApplicationIconBadgeNumber.
Which method listen to notifications when my App is in Background?
HEres my AppDelegate.cs:
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
PushNotificationManager.DidRegisterRemoteNotifications(deviceToken);
}
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
PushNotificationManager.RemoteNotificationRegistrationFailed(error);
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
PushNotificationManager.DidReceiveMessage(userInfo);
}
Actually apple push notification are handled by iOS and not your app. We can't change the badge on receiving a push notification. DidReceiveRemoteNotification is triggred only if user tap on the notification. so updating badge number in this method is not wise.
We can alter the badge number when we are in the background state by sending the "badge" parameter in the push notification package.
Sample payload:
{
"aps" : {
"alert" : "message",
"badge" : 2
}
}
your app icon will show 2. Here the point is that calculation for badge count should be done in the server side.
Note that the badge parameter in the payload must be an integer.

How to clear push notification from Ionic Push?

The push notification clears when the user taps on the notification to open app. However if the user goes and opens the app, the push notification is still there. How can I get rid of the notification? I can't seem to find anywhere in the documentation that addresses this.
Thanks a lot in advance
I did some digging as I too had this problem, and it looks like a bug in the Push Plugin. Basically they added the removal code to the pause event instead of the resume event.
You just have to change the code in src/android/com/plugin/gcm/PushPlugin.java
from:
#Override
public void onPause(boolean multitasking) {
super.onPause(multitasking);
gForeground = false;
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
#Override
public void onResume(boolean multitasking) {
super.onResume(multitasking);
gForeground = true;
}
to:
#Override
public void onPause(boolean multitasking) {
super.onPause(multitasking);
gForeground = false;
}
#Override
public void onResume(boolean multitasking) {
super.onResume(multitasking);
gForeground = true;
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
This will create a more standard behaviour, where the notifications will be removed on app resume (instead of on pause). Note though, I haven't fully tested the effects on the internal app messages yet, which may require more changes.

Resources