Hello,I want to open a different xaml page from android push notification click? But I have searched the forum and internet but cant find a working solution.Any ideas about how to achive this? (I'm using fcm integration in xamarin.forms project)
Below I passed job I'd, NotificationId parameters I mean push notification data to MainActivity.cs using PutExtra
var intent = new Intent(this, typeof(MainActivity));
intent.PutExtra("JobID", jobId); // Passed parameters to MainActivity.cs
intent.PutExtra("NotificationId", NotificationId);
intent.AddFlags(ActivityFlags.ClearTop);
Random random = new Random();
int pushCount = random.Next(9999 - 1000) + 1000; //for multiplepushnotifications
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, pushCount, intent, PendingIntentFlags.Immutable);
var notificationBuilder = new Notification.Builder(this)
.SetSmallIcon(Resource.Drawable.OneIcon)
.SetContentTitle(NotificationTitle)
.SetContentText(NotificationMessage)
.SetAutoCancel(true)
.SetDefaults(NotificationDefaults.Sound)
.SetContentIntent(pendingIntent);
var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify(pushCount, notificationBuilder.Build());
MainActivity.cs
Now Accessing those data in MainActivity.cs using GetStringExtra and passed one boolean value to the App.cs based on that boolean value I shall navigating the pages
Here accessing that boolean value and written some conditions
public class App : Application
{
public static string isNotified;
public static string isNotifiedId;
public bool navigating;
public bool IsNotified;
public App(bool shallNavigate)
{
navigating = shallNavigate;
}
protected override void OnStart()
{
OnAppStart();// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
private async void OnAppStart()
{
#region PushNotication
try
{
var objUserInfo = new DBMethods().GetUserInfo();//Here Iam accessing localDB
if (navigating == false) **// This condition for normar process and in else part is pushnotification process**
{
if (objUserInfo != null) // Local DB is Null normal process
{
if (Device.OS == TargetPlatform.iOS)// In iOS
{
MainPage = new HomeMasterPage();
}
else// In Android we have done some process
{
if (IsNotified != true)
{
MainPage = new HomeMasterPage();// HomePage
}
else
{
if (objUserInfo != null)
{
MainPage = new AppliedJobDetails(null, Constants.rem_Ids, " ");
IsNotified = false;
}
else
{
MainPage = new Carousel();// Start page
}
}
}
}
else
{
MainPage = new Carousel();// Start page
}
}
else
{
if (objUserInfo != null)
{
MainPage = new AppliedJobDetails(null, isNotified, isNotifiedId); **// PushNotification naviagating page**
}
else
{
MainPage = new Carousel(); //Start page
}
}
}
catch (Exception ex)
{
//System.Diagnostics.Debug.WriteLine ("Error => " + ex.Message);
//System.Diagnostics.Debug.WriteLine ("StackTrace =>" + ex.StackTrace);
throw new Exception(ex.Message);
}
#endregion
// Handle when your app starts
}
}
As you are showing/writing navigation in else block of UIApplicationState.Active , I wonder how it going to work, why because notifications tap events firing somewhere else.
I am using Xamarin.iOS, and notification tap event here firing.
public class CustomUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
** // Here you handle the user taps**
completionHandler();
}
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);
}
}
However I am getting notifications data here. but I dont have idea to navigate to specific page.
Please suggest me if you people have better idea.
So I want to have a notification open a new page, so the user can view the full message.
It works great with Xamarin.Android, you use the Notification.Builder(), and start a new Intent. voila
But with Xamarin.iOS, if the app is open , I have no luck.
Trying App.Current.MainPage.Navigation.PushModalAsync(new MyPage()) and it does nothing , it just stays at the current screen on the app
@VenkataSwamy ,I've got the error "**android.content.Context.getPackageName()' on a null object reference
**" at the line var intent = new Intent(this, typeof(MainActivity));,Can you please help me
Posts
Hi @slayer35,
I am not sure this is correct way or not but it's working for me
Xamarin.iOS:
App.Current.MainPage = new PageName(); put in DidReceiveRemoteNotification
Example:
AppliedJobDetails is the page name I mentioned
https://forums.xamarin.com/discussion/comment/287447#Comment_287447
Xamarin.Android:
AppGcmListenerService.cs
Below I passed job I'd, NotificationId parameters I mean push notification data to MainActivity.cs using PutExtra
MainActivity.cs
Now Accessing those data in MainActivity.cs using GetStringExtra and passed one boolean value to the App.cs based on that boolean value I shall navigating the pages
App.cs:
Here accessing that boolean value and written some conditions
If you have any doubts let me know
Thank you very much,This work well for me too.Thats just I need.
@BalaRaju
As you are showing/writing navigation in else block of
UIApplicationState.Active
, I wonder how it going to work, why because notifications tap events firing somewhere else.I am using Xamarin.iOS, and notification tap event here firing.
However I am getting notifications data here. but I dont have idea to navigate to specific page.
Please suggest me if you people have better idea.
Thank you
So I want to have a notification open a new page, so the user can view the full message.
It works great with Xamarin.Android, you use the Notification.Builder(), and start a new Intent. voila
But with Xamarin.iOS, if the app is open , I have no luck.
Trying App.Current.MainPage.Navigation.PushModalAsync(new MyPage()) and it does nothing , it just stays at the current screen on the app
Any ideas?
@VenkataSwamy, thanks for your solution. It works well.
Did you ever do saving the push notification in a local database?
__in android side you can use this way_
first in MainActivity
LaunchMode = LaunchMode.SingleTop_
and than_
protected override void OnNewIntent(Intent intent)
{
if (intent != null)
{
string NotificationId = Intent.GetStringExtra("NotificationId");
if (NotificationId != null)
{
App.IsNotified = true;
App.NotifiedId = NotificationId;
Hello @VenkataSwamy , @BasilJohn
How can get notification id and job id
@VenkataSwamy ,I've got the error "**android.content.Context.getPackageName()' on a null object reference
**" at the line
var intent = new Intent(this, typeof(MainActivity));
,Can you please help meHi @VenkataSwamy I am trying to open a content page when taps the notification.
What is AppGcmListenerService.cs? Is that invoke when we tap a notification? What are jobId and NotificationId? Is this work with FCM?
Can you help me to complete this implementation? I have started a new thread for this.