0

I have a Webview and for example : string url = "https://www.google.com"; UrlLoad(url); Show the Google in my App,

ok,If I received a notification and Push that, the Webview will Change page.

Just Like string url = Notification's body;

I'm sorry ,my English is not good.

Thanks.

ps. I'm use Xamarin

This is FCM Receive

[Service]

   [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]   
   class Firebasemessaging : FirebaseMessagingService
   {
    public override void OnMessageReceived(RemoteMessage message)
    {
        this.SendNotification(message.GetNotification().Body);
        Notification notifi = new 
    Notification(Resource.Drawable.pika,"Handbim");

    }

    private void SendNotification(string body)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
        var defaultsoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
        var notif = new NotificationCompat.Builder(this)
            .SetContentTitle("handbim")
            .SetContentText(body)
            .SetAutoCancel(true)
            .SetSound(defaultsoundUri)
            .SetContentIntent(pendingIntent);

      var notificationManager = NotificationManager.FromContext(this);
        notificationManager.Notify(0,notif.Build());

    }
}
Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39
DougZo
  • 23
  • 9
  • Do you mean that you will got notification from FCM and then you will get the notification's body and then you will show the body's content on your `WebView`? Your question is not clear for us. – Robbit Jan 01 '18 at 07:00
  • Yes,so the body's content its a URL – DougZo Jan 02 '18 at 01:38

1 Answers1

0

Please use OnNewIntent to achieve your goal.

1) Add LaunchMode =Android.Content.PM.LaunchMode.SingleTop in your MainActivity's attribute, like this:

[Activity(Label = "StartStopService", MainLauncher = true,LaunchMode =Android.Content.PM.LaunchMode.SingleTop)]

2) In your SendNotification method, add this line:

 intent.PutExtra("url", body);

So the body will be passed to your OnNewIntent method by intent.

3) Override OnNewIntent method in your MaintActivity, like this:

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);

    string str = intent.GetStringExtra("url");
    Toast.MakeText(this, str, ToastLength.Short).Show();
    web_view.LoadUrl(str);
}

Note:

web_view is global variables

Robbit
  • 4,300
  • 1
  • 13
  • 29
  • Thank you ,sir. But I Gave up use Xamarin,and Kotlin now. Thanks again. – DougZo Jan 08 '18 at 03:36
  • Kotlin and Xamarin, both of them can be used to develop Android application, or maybe you can use java. But the question you have asked is same in different language. You can try my solution, and leave me a comment if you success or fail, I will help you. – Robbit Jan 08 '18 at 04:45
  • I am Sorry,My mistake ,I mean its i'm USING kotlin Now,lol. actually,I'm tryed,but not work.I just no idea for Background Notification.I want get body's values when I click the Notification. – DougZo Jan 08 '18 at 07:55
  • Is there any error? Because I have tried it, maybe there is something wrong in other code, you can try it in a new demo and compare it with your origin project. – Robbit Jan 08 '18 at 07:59
  • excuz sir,Where I put the [intent.GetStringExtra("url")] – DougZo Jan 08 '18 at 09:02
  • In your `OnNewIntent` method. – Robbit Jan 08 '18 at 09:04
  • sorry,its [intent.putString("url",body);] :( – DougZo Jan 08 '18 at 09:16
  • `var intent = new Intent(this, typeof(MainActivity));` below it in your `SendNotification` method. – Robbit Jan 08 '18 at 09:19
  • You mean that you are using firebase to send notification, and your application is background, and then when you click the notification, you can't refresh the `WebView` or it can't start your application? – Robbit Jan 08 '18 at 09:38
  • but that's my want too. – DougZo Jan 08 '18 at 09:41
  • I suggest you open a new thread about refresh `WebView` when application is background, it is not in this question's scope. You question about this is just to refresh the `WebView`. – Robbit Jan 08 '18 at 09:45
  • I can open my app,but I want click Notification from FCM then refresh WebView with fcm's body(it's a website) When my app running on background. – DougZo Jan 08 '18 at 09:46
  • I'm add Toast. when app run on background the getStringExtra("url") its "empty",but run on foreground will show getStringExtra("url"). I'm sorry, I'm newbie for App-develop. – DougZo Jan 08 '18 at 09:52
  • `when app run on background the getStringExtra("url") its "empty"`, you just need add `getStringExtra("url")` in your `OnCreate` method, and judge it's value and do something. If it is null, don't do anything, else refresh your webview – Robbit Jan 08 '18 at 09:56
  • Because your activity has been recreated when it is background and it won't call `OnNewIntent`, so you get "empty". – Robbit Jan 08 '18 at 09:58
  • Ok, after trying it, please leave me a comment, thank you. – Robbit Jan 08 '18 at 13:58
  • hello sir,its same,I'm tried to add getStringExtra("url") in onCreate(),but still empty,and I saw other one use that https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase ,but I dont know how to do. – DougZo Jan 10 '18 at 02:56
  • If you use `Display Messages`, you can't get the notification when your phone is background, but if you use `Data Messages`, you can get the message when your phone is foreground/background/killed, but it don't provide a UI to show the message. You need choose which way you want to send you message. – Robbit Jan 10 '18 at 03:01
  • the "Data Messages" is send notification via FCM API? like post something to https://fcm.googleapis.com/fcm/send ? – DougZo Jan 10 '18 at 03:34
  • If you feel that I have helped you, please give me a vote up or accept it, thank you! – Robbit Jan 10 '18 at 08:46
  • Hello sir,Can I if want use Data messages in onStart or onRestart? – DougZo Jan 18 '18 at 08:39
  • Application is background, and you are using Data message, and you want get message in `OnStart` or `OnRestart`? Has `OnStart`/`OnRestart` method been called? – Robbit Jan 18 '18 at 08:51
  • Yes,because when I click device's home button,it will be background ,then when I click Notification(data messages) it will not call `onCreate`,then I try add `getIntent.extra` into `onRestart` and `onStart`, but not working. – DougZo Jan 18 '18 at 09:07
  • So, `onCreate`, `OnNewIntent`, `onRestart` and `onStart`, all of them aren't called when you click the notification and back to your application? It is impossible, what about the `OnResume` method? – Robbit Jan 18 '18 at 09:16
  • When application NO run,`onCreate` is work good,its successful work in **no run** and **foreground**. only push Home button not work. – DougZo Jan 18 '18 at 09:33
  • Ok, check if this `SendNotification` method has been called when you click the notification. – Robbit Jan 18 '18 at 09:41
  • Please put aside other things, let us come to an ongoing discussion. I am waiting for you. – Robbit Jan 18 '18 at 09:43
  • No,In my project's `SendNotification` method hava setting `smallicon(not same with app's icon)`,so I guess it via data messages,it show Application's icon. – DougZo Jan 19 '18 at 01:51
  • Or Can I call `onDestroy()`? Is it recommend or not? I no hava common sense about app develop – DougZo Jan 19 '18 at 02:47
  • @DougZo,I try to solve this problem by Broadcast Receiver – engmms Nov 10 '19 at 22:29