未显示本地通知 [英] Local notifications not shown
本文介绍了未显示本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
帮我找出代码中的错误我的主要活动:
Help me find the error in the code My MainActivity:
string CHANNEL_ID = "MyChannel";
protected override void OnCreate(Bundle savedInstanceState)
{
....
...
CreateNotificationChannel();
LoadApplication(new App());
}
public void CreateNotification(string SetContentTitle, string SetContentText)
{
var builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.SetContentTitle(SetContentTitle)
.SetContentText(SetContentText)
.SetChannelId(CHANNEL_ID)
.SetSmallIcon(Resource.Drawable.icon);
// Finally, publish the notification:
var notificationManager = NotificationManagerCompat.From(this);
// Get the notification manager:
// NotificationManager notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
// Publish the notification:
int notificationId = 1000;
notificationManager.Notify(notificationId, builder.Build());
}
void CreateNotificationChannel(){
if (Build.VERSION.SdkInt<BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
string name = "MyName";
var description = "desc";
var channel = new NotificationChannel(CHANNEL_ID, name, NotificationImportance.High)
{
Description = description
};
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
我有一个类,当我需要时,我开始使用下面的代码显示本地通知
and I have a class in which when I need I start displaying local notifications using the code below
var urlOb = new MainActivity();
urlOb.CreateNotification(titleback, textback);
我认为我做的一切都正确,但未显示本地通知.
I think I did everything right, but local notifications are not shown.
推荐答案
可以在其他activity中调用方法
You can call the method in other activity
public static MainActivity instance;
在方法onCreate
//...
base.OnCreate(savedInstanceState);
instance = this;
你可以用其他方法调用它
And you can call it in other method
MainActivity.instance.CreateNotification(titleback, textback);
这篇关于未显示本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文