帮我找到代码中的错误 我的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);
}
我有一堂课,当我需要的时候,我开始使用下面的代码显示本地通知
var urlOb = new MainActivity();
urlOb.CreateNotification(titleback, textback);
我认为我做对了所有事情,但是未显示本地通知。
答案 0 :(得分:1)
您可以在其他活动中调用该方法
public static MainActivity instance;
在方法onCreate
//...
base.OnCreate(savedInstanceState);
instance = this;
您可以用其他方法调用
MainActivity.instance.CreateNotification(titleback, textback);