Hello @Vuyiswa Maseko ,
Welcome to our Microsoft Q&A platform!
Java.Lang.NoSuchMethodError: no non-static method "Landroid/app/Notification$Builder;
The Notification.Builder
command is deprecated in Api 23, try using the NotificationCompat.Builder
method instead. And since Android 8.0, sending a notification needs the NotificationChannel
.
Here is the sample code, you could refer to it.
private void DispatchNotificationThatAlarmIsGenerated(string message)
{
var intent = new Intent(this, typeof(MainActivity));
...
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
if (Build.VERSION.SdkInt > BuildVersionCodes.O)
{
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channelId")
.SetContentTitle("Alarm")
.SetContentText(message)
.SetSmallIcon(Resource.Drawable.abc)
.SetAutoCancel(true);
var chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, GetString(Resource.String.abc_action_bar_home_description), NotificationImportance.Default);
chan.LightColor = Android.Graphics.Color.Green;
chan.LockscreenVisibility = NotificationVisibility.Private;
notificationManager.CreateNotificationChannel(chan);
notificationManager.Notify(NOTIFICATION_AlARM_ID, notificationBuilder.Build());
}
else
{
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.SetDefaults((int)NotificationDefaults.All)
.SetSmallIcon(Resource.Drawable.abc)
.SetVibrate(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 })
.SetSound(null)
.SetPriority(NotificationCompat.PriorityDefault)
.SetAutoCancel(false)
.SetContentTitle("Mobile")
.SetContentText("My service started")
.SetOngoing(true);
notificationManager.Notify(NOTIFICATION_ID, notificationBuilder.Build());
}
}
Best Regards,
Jarvan Zhang
If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.