Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification doesn't receive click event when the activity is closed #123

Closed
R2D221 opened this issue May 30, 2017 · 6 comments
Closed

Notification doesn't receive click event when the activity is closed #123

R2D221 opened this issue May 30, 2017 · 6 comments

Comments

@R2D221
Copy link

R2D221 commented May 30, 2017

I'm using this plugin to show notifications in many scenarios. One of them is push notifications. When I receive a push notification I show a toast using the service DependencyService.Get<IToastNotificator>(); and specifying IsClickable = true.

If the activity is open when the notification is clicked, I get the callback as expected, but I close the activity in the task manager and then click the notification, nothing happens.

More specifically, if I run the debugger, I get that result.Action is Timeout, no matter if I click or dismiss the notification.

I believe this has something to do with the fact that NotificationBuilder registers receivers against the activity, but if the activity is closed, then there's nothing that can receive the events.

@Greg767
Copy link

Greg767 commented Jul 25, 2017

A workaround to this can be that when you create the notification you set a flag. If the user taps directly on the notification when the app is still open you do your action and you unset the flag. If the app goes background without the notification tapped than when the app returns you check the flag and if its set then you know that there was a notification so you need to do the action..

@R2D221
Copy link
Author

R2D221 commented Jul 26, 2017

That doesn't sound right. What happens if:

  • I receive the notification
  • I close the app
  • I launch the app from the home screen?

I solved my problem just creating native Android notifications, but it would be great if this was possible using this plugin as well.

@Greg767
Copy link

Greg767 commented Jul 26, 2017

You are right, I missed this case.

@AlcantaraRW
Copy link

Facing the same problem here. I tried to implement native Android notifications too but couldn't get it to work...

@Greg767
Copy link

Greg767 commented Oct 19, 2017

I needed local notifications so what I did is to create a dependency service where I call the following code when the app is running in the foreground:
`//type needs to be derived from java, not xamarin.forms
Intent LoadPostPage = new Intent(Forms.Context, typeof(MainActivity));
LoadPostPage.PutExtra("NotificationTrue", true);
//JackpotClickNotificationId is only a constant
const int pendingIntentId = JackpotClickNotificationId;
PendingIntent pendingIntent =
PendingIntent.GetActivity(Forms.Context, pendingIntentId, LoadPostPage, PendingIntentFlags.UpdateCurrent);

		// Build the notification:
        NotificationCompat.Builder builder = new NotificationCompat.Builder(Forms.Context)
            .SetContentIntent(pendingIntent)
			.SetAutoCancel(true)                    // Dismiss from the notif. area when clicked
            .SetContentTitle(AppTextsFr.JackpotNotificationTitle)      // Set its title
            .SetSmallIcon(Resource.Drawable.notificationwhite)  // Display this icon
            .SetLargeIcon(BitmapFactory.DecodeResource(Forms.Context.Resources, Resource.Drawable.icon180))
            .SetContentText(AppTextsFr.JackpotNotificationMessage); // The message to display.

        // Finally, publish the notification:
        NotificationManager notificationManager = (NotificationManager)Forms.Context.GetSystemService(Context.NotificationService);
		notificationManager.Notify(JackpotClickNotificationId, builder.Build());`

This code will display a local notification that will stay in the notifications drawer as long as the app is running (even in the background). When we click on the notification then in MainActivity I do the following check in OnResume
if (Intent != null && Intent.HasExtra("NotificationTrue")) { App.OpenJackpotAfterStartup = true; }
So when the app has come back you can do whatever you want because you set a flag.
For my use-case this behavior is enough.

@adamped
Copy link
Contributor

adamped commented Nov 25, 2017

You can now click notifications from the NotificationCenter and have it continue the code in your app, if you use the Notify method with the Callback. (Starting with 3.3.0-pre1) Marking this as closed. Please comment again if the issue is still there, or create a new issue if it works but has a bug.

@adamped adamped closed this as completed Nov 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants