1

i have implemented Local Notifications in Xamarin.Forms using Xam.Plugins.Notifier. I have added the Nuget Package in PCL, iOS and Android. below code is displaying the notification. i wrote the below chunk of code in my PCL

CrossLocalNotifications.Current.Show("test" + " " + " New test", "Some Notification", 5,DateTime.Now.AddSeconds(8));

I have added the below code in AppDelegate.CS

if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
        {
            // iOS 10.0+
            UNUserNotificationCenter.Current.RequestAuthorization(
                UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
                (approved, error) => { });

            UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();
        }

I am able to get the notification in iOS but am not getting the notification in Android. Do i need add any code in MainActivity.cs can someone help me to fix this?

FYI: https://github.com/edsnider/LocalNotificationsPlugin

Santhosh Kumar
  • 199
  • 3
  • 16
  • To receive notification by Android project you need create a receiver in the android manifest, at least using Firebase – pnet Dec 11 '17 at 11:14

1 Answers1

1

As the document said, your code :

CrossLocalNotifications.Current.Show("test" + " " + " New test", "Some Notification", 5, DateTime.Now.AddSeconds(8));

It will display a local notification at a scheduled date/time, your notification will display after 8s delay.

If you want display a local notification immediately, you could try using :

CrossLocalNotifications.Current.Show("title", "body");

It works fine on my side.

Update :

You were missing something to display a notification .

As per the official documentation: a Notification object must contain the following:

  1. A small icon, set by SetSmallIcon()

  2. A title, set by SetContentTitle()

  3. Detail text, set by SetContentText()

See http://developer.android.com/guide/topics/ui/notifiers/notifications.html

York Shen
  • 9,014
  • 1
  • 16
  • 40
  • Hey thanks for your reply. I wantedly made it for 8sec. My problem is the notifications are not firing up in Android. Do i need to write any code in MainActivity.cs like i wrote it for AppDelegate.CS? Can you please help me out. – Santhosh Kumar Dec 08 '17 at 04:49
  • When i debug it am getting this exception. Its coming in android only not in iOS. {System.NotImplementedException: The method or operation is not implemented. at Plugin.LocalNotifications.CrossLocalNotifications.get_Current () [0x00012] in :0 at – Santhosh Kumar Dec 08 '17 at 04:52
  • @SanthoshKumar, please post the complete error message. – York Shen Dec 08 '17 at 05:12
  • @SanthoshKumar, and how did you use the `CrossLocalNotifications.Current.Show` method ? please post your complete code. – York Shen Dec 08 '17 at 05:15
  • Hey Thanks for your reply again. I missed to add the nuget package for Droid Project. now the notifications are firing up. One question here. Why it is not asking for the Request to Allow or Don't allow Pop up to display the notifications like how iOS app asks it – Santhosh Kumar Dec 08 '17 at 05:26
  • Even it is not showing the toast in the app. When i launch the notification bar from the top then i can ale to see the notifications. Do we need to add any code to get this ? – Santhosh Kumar Dec 08 '17 at 05:29
  • @SanthoshKumar, actually the Push Notification permission lie in Normal Category Permission not in Dangerous Category Permission, so you could directly use it. – York Shen Dec 08 '17 at 05:30
  • Okay. But how to display the toast in the app in the top, rather than pulling the notification bar and see it ? how to achieve this? – Santhosh Kumar Dec 08 '17 at 05:38
  • @SanthoshKumar, you could refer to : https://stackoverflow.com/questions/29949501/android-show-notification-with-a-popup-on-top-of-any-application – York Shen Dec 08 '17 at 05:46
  • @ York Shen, I wrote the below code in MainActivity.cs but its not showing up NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.SetContentTitle("VLOnMobile") .SetContentInfo("Licenses are Going to Expire") .SetPriority((NotificationCompat.PriorityHigh)); NotificationManager mNotificationManager = (NotificationManager)GetSystemService(Context.NotificationService); mNotificationManager.Notify(0, builder.Build()); – Santhosh Kumar Dec 08 '17 at 06:02
  • @SanthoshKumar, I have update my answer, you may check it. – York Shen Dec 08 '17 at 06:10
  • Thank you so much, I wrote the below code, they are coming but not showing as Toast NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.SetContentTitle("VLOnMobile") .SetContentInfo("Licenses are Going to Expire") .SetSmallIcon(Resource.Drawable.icon) .SetContentText("Hello World!") .SetPriority((NotificationCompat.PriorityHigh)); – Santhosh Kumar Dec 08 '17 at 06:28
  • NotificationManager mNotificationManager = (NotificationManager)GetSystemService(Context.NotificationService); mNotificationManager.Notify(0, builder.Build()); – Santhosh Kumar Dec 08 '17 at 06:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160767/discussion-between-york-shen-msft-and-santhosh-kumar). – York Shen Dec 08 '17 at 06:29
  • @SanthoshKumar, you could open a new question for a better solution. : ) – York Shen Dec 08 '17 at 06:29
  • Can you come to chat? – Santhosh Kumar Dec 08 '17 at 06:32
  • @SanthoshKumar, could you please mark it as an answer ? – York Shen Dec 12 '17 at 05:22