简体   繁体   English

Xamarin.Forms Android本地通知未发送

[英]Xamarin.Forms Android Local Notification Not Sending

I have followed the Xamarin walkthrough, and it's not working for me. 我已经遵循了Xamarin演练,但对我来说不起作用。 The code falls through this cleanly, but it never sends the notification. 代码完全通过了此过程,但是它从不发送通知。 It never shows up on my emulator or device. 它永远不会显示在我的模拟器或设备上。

I have no idea what is going on. 我不知道发生了什么。

    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());

Any help or links would be very helpful. 任何帮助或链接将非常有帮助。 I'm just completely lost; 我完全迷路了; been working on this for about 14 hours now, and cannot find any help on the Google. 已经为此工作了大约14个小时,并且在Google上找不到任何帮助。

Answer to my inquiry: You must have an Icon set for notifications to be properly build and sent. 回答我的问题:您必须设置一个图标,才能正确构建和发送通知。 Though, it won't send an error for not having one. 虽然,它不会因为没有一个而发送错误。

Short version: Needed to add 简短版本:需要添加

 .SetSmallIcon(Resource.Drawable.icon);

Add an icon to notification. 在通知中添加图标。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM