xamarin.android通知不出现

时间:2018-11-07 09:27:20

标签: c# notifications xamarin.android

我正在尝试发出一个简单的通知,但是到目前为止,它尚无用。我有一个只有一个按钮的简单应用程序,当我单击该按钮时,我想推送通知。

这是我的点击事件:

private void Notificate(object sender, EventArgs e)
    {
        Intent i = new Intent(this, typeof(MainActivity));
        PendingIntent resultPending = PendingIntent.GetActivity(this, 0, i, PendingIntentFlags.CancelCurrent);


        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .SetAutoCancel(true)
            .SetContentIntent(resultPending)
            .SetSmallIcon(Resource.Drawable.notification_icon_background)
            .SetContentTitle("Notification title")
            .SetContentText("My notification text");

        NotificationManager manager = (NotificationManager)GetSystemService(Context.NotificationService);
        manager.Notify(100, builder.Build());
    }  

我了解到通知可能已关闭,但是我用NotificationManagerCompat.From(this).AreNotificationsEnabled()进行了检查,并说通知已打开。我也没有任何错误。

我想念什么?

1 个答案:

答案 0 :(得分:1)

我建议您这样做:

  • 声明待处理的意图(在单击通知时触发此意图)

     Intent intent = new Intent(this, typeof(MainAcitivity)); //Activity you want to open
     intent.AddFlags(ActivityFlags.ClearTop);
     var pendingIntent = PendingIntent.GetActivity(this, RandomGenerator(), intent, PendingIntentFlags.OneShot);
    
  • 如果您想要默认声音:

    Notification notify = new Notification();
    notify.Defaults = NotificationDefaults.Sound;
    notify.Defaults = NotificationDefaults.Vibrate;
    
  • 使用NotificationCompat推送通知(用于向后兼容)

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
         .SetSmallIcon(Resource.Drawable.Icon)
         .SetContentTitle(messageTitle)
         .SetContentText(messageBody)
         .SetSound(Settings.System.DefaultNotificationUri)
         .SetVibrate(new long[] { 1000, 1000 })
         .SetLights(Color.AliceBlue, 3000, 3000)
         .SetAutoCancel(true)
         .SetContentIntent(pendingIntent);
    
      if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                String channelId = Context.GetString(Resource.String.default_notification_channel_id);
                NotificationChannel channel = new NotificationChannel(channelId, MessageTitle, NotificationImportance.Default);
                channel.Description=(MessageBody);
                builder.SetChannelId(channelId);
            }
    
  • 然后通知系统您的应用程序发出了通知:

       NotificationManager notificationManager = (NotificationManager)Context.GetSystemService(Context.NotificationService);                             
       notificationManager.CreateNotificationChannel(channel);
       notificationManager.Notify(RandomGenerator(), notificationBuilder.Build());
    
  • 此外,还使用随机数,以便在引发新通知时通知不会切换位置:

    private int RandomGenerator() 
    {
       return new Random().Next( int.MinValue, int.MaxValue );
    }
    
  • 要实现Android Oreo频道兼容性(如您所见,official docs,您需要在应用程序组件的AndroidManifest.xml中添加以下元数据元素:

    <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id"/>
    
  • 当通知消息中没有指定通道,或者应用尚未创建提供的通道时,将使用此默认通道。