首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我按下按钮后,电话里没有显示通知

我按下按钮后,电话里没有显示通知
EN

Stack Overflow用户
提问于 2019-12-30 18:27:18
回答 1查看 52关注 0票数 0

我已经写了下面的代码,据称,根据我读到的内容,它应该在我按下按钮后立即显示通知,但通知没有弹出。有人能给我解释一下原因吗?谢谢。我已经在下面添加了活动和布局的代码。

这就是活动

代码语言:javascript
复制
namespace PresentationNoti
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        Button btn1, btn2;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            btn1 = (Button)FindViewById(Resource.Id.btn1);
            btn2 = (Button)FindViewById(Resource.Id.btn2);
            btn1.Click += Btn1_Click;
            btn2.Click += Btn2_Click;
        }

        private void Btn2_Click(object sender, System.EventArgs e)
        {
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
            mBuilder.SetSmallIcon(Resource.Drawable.birthday);
            mBuilder.SetContentTitle("It's your birthday");
            mBuilder.SetContentText("Happy B-day Birthday Boy/Girl");
            Intent resultIntent = new Intent(this, typeof(MainActivity));
            TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
            stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity)));
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.AddNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
            mBuilder.SetContentIntent(resultPendingIntent);
            NotificationManager mNotificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
            mNotificationManager.Notify(0, mBuilder.Build());
        }

        private void Btn1_Click(object sender, System.EventArgs e)
        {
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
            mBuilder.SetSmallIcon(Resource.Drawable.birthday);
            mBuilder.SetContentTitle("It's your birthday");
            mBuilder.SetContentText("Happy B-day Birthday Boy/Girl");
            NotificationManager mNotificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
            mNotificationManager.Notify(0, mBuilder.Build());
        }
    }
}

这是axml。

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#90aacc">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="basic notification"
        android:textColor="#000000"
        android:layout_marginTop="15dp"
        android:layout_gravity="center"
        android:textSize="25sp"
        android:id="@+id/btn1"
    />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="advanced notification"
        android:textColor="#000000"
        android:layout_marginTop="15dp"
        android:layout_gravity="center"
        android:textSize="25sp"
        android:id="@+id/btn2"
    />
</LinearLayout>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-30 19:08:45

根据Xamarin的文档,您应该创建一个通知通道(API >= 26),并使用以下代码启动通知:

代码语言:javascript
复制
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(NOTIFICATION_ID, builder.Build());

编辑:如果你正在为API 26或更高版本编译,则创建通知通道(在这种情况下是必需的)。

附言: Full xamarin的安卓通知文档here

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59529278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档