Xamarin.Forms Android本地通知不发送

问题描述:

我遵循Xamarin演练,它不适合我。代码通过这干净,但它永远不会发送通知。 它永远不会出现在我的模拟器或设备上。Xamarin.Forms Android本地通知不发送

我不知道发生了什么事。

public override void OnReceive(Context context, Intent intent) 
    { 

     string message = intent.GetStringExtra("message"); 
     string title = intent.GetStringExtra("title"); 
     int id = int.Parse(intent.GetStringExtra("id")); 

     //Generate a notification with just short text and small icon 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
         .SetAutoCancel(true)     // Dismiss from the notif. area when clicked 
         .SetContentTitle(title)  // Set its title 
         .SetContentText(message); // The message to display. 


     NotificationManager notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService); 
     notificationManager.Notify(id, builder.Build()); 

任何帮助或链接都会非常有帮助。我完全迷失了;现在已经工作了大约14个小时,并且在Google上找不到任何帮助。

回答我的问题:您必须设置一个图标,才能正确构建和发送通知。虽然,它不会因没有一个而发出错误。

短版:需要添加

.SetSmallIcon(Resource.Drawable.icon); 

添加一个图标来通知。

Notification.Builder builder = new Notification.Builder (this) 
.SetContentTitle ("Title") 
.SetContentText ("Message") 
.SetSmallIcon (Resource.Drawable.ic_notification); 

Notification notification = builder.Build(); 

NotificationManager notificationManager = 
     GetSystemService (Context.NotificationService) as NotificationManager; 

const int notificationId = 0; 
notificationManager.Notify (notificationId, notification); 
+0

啊!我明白了,你需要有一个图标。我没有想过这个。这只是一个功能测试。 – Hyren123