Xamarin.Android通知详解

时间:2023-03-09 03:25:36
Xamarin.Android通知详解

一、发送通知的机制

在日常的app应用中经常需要使用通知,因为服务、广播后台活动如果有事件需要通知用户,则需要通过通知栏显示,而在Xamarin.Android下的通知需要获取NotificationManager服务,而该服务需要通过GetSystemService获取,同时还要传递一个标识符。获取了通知管理器后我们就可以实例化Notification,然后再由NotificationManager发送出去。这就是整个过程了。下面我们将一一详解通知。

二、前期准备

为了下面的学习和演示我们需要做好一些前期的准备

1.打开Main.axml删除上面的控件

2.打开MainActivity.cs文件并写入以下内容

     [Activity(Label = "NotificationStudy", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private NotificationManager nMgr; protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main); //获取通知管理类
nMgr = (NotificationManager)GetSystemService(NotificationService);

3.右击项目-》属性然后按照如下所示加上能够控制振动的权限

Xamarin.Android通知详解

三、发送普通通知

首先我们打开Main.axml文件,然后设置如下一个按钮:

Xamarin.Android通知详解

并设置id为@+id/normalButton

MainActivity.cs先获取按钮对象:

 Button normalButton = FindViewById<Button>(Resource.Id.normalButton);

监听normalButton按钮的点击事件:

             normalButton.Click += (e, s) =>
{
//设置通知的图标以及显示的简介Title
Notification notify = new Notification(Resource.Drawable.Icon, "普通通知");
//初始化点击通知后打开的活动
PendingIntent pintent = PendingIntent.GetActivity(this, , new Intent(this, typeof(MainActivity)), PendingIntentFlags.UpdateCurrent);
//设置通知的主体
notify.SetLatestEventInfo(this, "普通通知标题", "普通通知内容", pintent);
//发送通知
nMgr.Notify(, notify);
};

其中我们先实例化了一个Notification对象,并设置其图标以及Ticker文字:

 Notification notify = new Notification(Resource.Drawable.Icon, "普通通知");

当然众所周知当我们点击通知之后都会打开对应的活动,所以我们需要初始化一个延迟意图,以便通知可以打开:

 PendingIntent pintent = PendingIntent.GetActivity(this, , new Intent(this, typeof(MainActivity)), PendingIntentFlags.UpdateCurrent);

这个PendingIntent仅仅只是Intent的一个封装。最后是设置通知的标题和内容以及对应的活动,最后就是发送这个通知,再发送通知的Notify方法中第一个参数为该通知的ID,有了这个ID后面就可以取消这个通知。

下面我们测试,发送该通知:

Xamarin.Android通知详解

四、取消通知

通过上面的代码我们已经发送了一个通知,那么我们还需要关闭这个通知,这里我们再在Main.axml中添加一个按钮:

Xamarin.Android通知详解

并设置其id为@+id/cancelNotifyButton

打开MainActivity.cs文件,并绑定监听事件:

             Button cancelNotifyButton = FindViewById<Button>(Resource.Id.cancelNotifyButton);
cancelNotifyButton.Click += (e, s) =>
{
//根据id取消通知
nMgr.Cancel();
};

然后发送一个通知后,点击取消普通通知,会发现通知栏中不存在我们发送的通知了。

五、发送带有声音、震动和LED灯的通知

首先我们还是要在Main.axml中添加一个按钮:

Xamarin.Android通知详解

并设置它的id为@+id/lsvButton

打开MainActivity.cs并写入如下代码:

             Button lsvButton = FindViewById<Button>(Resource.Id.lsvButton);
lsvButton.Click += (e, s) =>
{
Notification notify = new Notification(Resource.Drawable.Icon, "带有声音、LED光和震动的通知");
//设置该通知具有声音、LED光和震动
notify.Defaults = NotificationDefaults.All; //获取系统默认的通知声音
Android.Net.Uri ringUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
//设置通知的声音
notify.Sound = ringUri; //设置一秒的震动
notify.Vibrate = new long[] { }; //设置LED的颜色为绿色
notify.LedARGB = Color.Green;
//设置LED显示时间为1s
notify.LedOnMS = ;
//设置LED熄灭时间为1s
notify.LedOffMS = ;
//设置标志位,否则无法显示LED
notify.Flags = NotificationFlags.ShowLights | notify.Flags; PendingIntent pintent = PendingIntent.GetActivity(this, , new Intent(this, typeof(MainActivity)), ); notify.SetLatestEventInfo(this, "标题", "内容", pintent); nMgr.Notify(, notify);
};

下面我们来分析一下代码,既然这个通知带有声音等,那么我们需要设置Defaults

 notify.Defaults = NotificationDefaults.All;

因为笔者使用了所有,所以直接是All,当然还可以是SoundVibrateLights的组合

为了能够贴近系统,所以我们并没有设置个性的声音,而是获取了系统本身的通知声音,下面的代码就是获取代码:

 Android.Net.Uri ringUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);

最后是将这个声音赋给通知:

 notify.Sound = ringUri;

然后就是震动:

 notify.Vibrate = new long[] {  };

笔者设置的是震动一秒,当然这是一个数组。而规则就是 震动的毫秒数,静止的毫秒数,震动的毫秒数…这种规则

最后就是比较麻烦的LED,首先是指定LED灯的颜色(如果不存在该颜色,系统会选择临近的颜色):

 notify.LedARGB = Color.Green;

然后笔者设置了显示的毫秒数与熄灭的毫秒数:

                 //设置LED显示时间为1s
notify.LedOnMS = ;
//设置LED熄灭时间为1s
notify.LedOffMS = ;

为了能够确保LED能够显示我们还需要设置Flags

 notify.Flags = NotificationFlags.ShowLights | notify.Flags;

最后发送通知(模拟器上看不出来,建议真机测试

六、通过Builder发送通知

这个方式相对于Notification更简单,也更快捷,但是只有在Android 3.0以上才支持(面对如今都是Android 4.0以上应该没有问题了

先在Main.axml中添加对应的按钮:

Xamarin.Android通知详解

并设置对应的id为@+id/builderButton

最后就是MainActivity.cs中的代码:

             Button builderButton = FindViewById<Button>(Resource.Id.builderButton);
builderButton.Click += (e, s) =>
{
var pintent = PendingIntent.GetActivity(this, , new Intent(this, typeof(MainActivity)), ); var notify = new Notification.Builder(this)
.SetTicker("来自Builder的通知") //设置通知的简介文字
.SetSmallIcon(Resource.Drawable.Icon) //设置通知的图标
.SetDefaults(NotificationDefaults.All) //设置该通知具备声音、LED和震动
.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification)) //设置通知声音
.SetVibrate(new long[] { }) //设置震动频率
.SetLights(Color.Red, , ) //设置LED
.SetContentTitle("标题") //设置标题
.SetContentText("内容") //设置内容
.SetContentInfo("信息") //设置右下角显示的文字
.SetAutoCancel(true) //点击该通知后是否自动消失
//通过 SetLargeIcon 可以设置大图标
.SetContentIntent(pintent); nMgr.Notify(, notify.Notification);
};

这里我们可以看到通过Builder方式创建一个通知是多么的方便,只要通过SetXXXXX就可以完成了,最后只要在发送通知的时候传入其Notification属性即可,当然这里很多的方法跟通过Nitification是一样的就不单独解释了,而且也有注释说明。

七、进度条式通知

大家在下载文件的时候,都会看到通知栏会有当前下载任务的进度,而这个通知可以通过Builder方式实现,而且更快捷。

Main.axml中添加对应的按钮

Xamarin.Android通知详解

并设置id为@+id/builderButton

其次就是MainActivity.cs文件

             Button progressButton = FindViewById<Button>(Resource.Id.progressButton);
progressButton.Click += (e, s) =>
{
var notify = new Notification.Builder(this)
.SetTicker("进度条通知")
.SetSmallIcon(Resource.Drawable.Icon)
.SetOngoing(true) //设置该通知是否可以被用户移除 true 表示不可以
.SetNumber() //设置该同时的条数 会在右下角显示数量
.SetContentTitle("标题")
.SetProgress(, , true); //第三个参数如果设置为true则进度条变成不确定进度条 nMgr.Notify(, notify.Notification);
};

这里主要使用了SetProgress方法来设置进度条的最大值,当前值。最后一个参数设置为true则表示该进度条为不确定进度条,就是只会显示滑动,不会显示当前的进度。

 SetProgress(, , true);

这里我们还使用了一个新方法SetOngoing,通过前面的通知,大家会发现这些通知用户完全可以通过手动的方式移除掉,但是我们如何创建一个用户无法移除的通知呢?就是通过使用SetOngoing方法,并传入一个true即可。

下面为实际运行图:

Xamarin.Android通知详解

八、自定义视图通知

大家在使用音乐相关的应用的时候会发现通知栏会有一个简单的功能界面,有当前歌曲的名称,专辑图片和暂停,下一曲等按钮,这些当然不是通知自带的,而是通过自定义通知来实现的,而本节我们不仅仅会学习如何使用自定义通知,同时还会学习如何设置自定义通知中控件的值,以及绑定监听事件。

首先我们在Main.axml中添加按钮

Xamarin.Android通知详解

设置其id为@+id/customeViewButton

既然是自定义视图,当然还需要一个视图,所以我们在Resources/layout下新建一个视图,并命名为NotificationCustomeView,并在其中写入如下的xml:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:id="@+id/relativeLayout1"
p1:padding="5dp">
<ImageView
p1:src="@drawable/Icon"
p1:layout_width="wrap_content"
p1:layout_height="match_parent"
p1:id="@+id/imageView1" />
<RelativeLayout
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:layout_toRightOf="@id/imageView1"
p1:id="@+id/relativeLayout2"
p1:paddingLeft="10dp">
<TextView
p1:text="Large Text"
p1:textAppearance="?android:attr/textAppearanceLarge"
p1:layout_width="match_parent"
p1:layout_height="wrap_content"
p1:id="@+id/ncvTextView" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:layout_below="@id/ncvTextView"
p1:id="@+id/ncvProgressBar"
p1:indeterminateOnly="false"
p1:indeterminate="false" />
</RelativeLayout>
</RelativeLayout>

当然最终的结果图如下所示:

Xamarin.Android通知详解

打开MainActivity.cs文件

             Button customeViewButton = FindViewById<Button>(Resource.Id.customeViewButton);
customeViewButton.Click += (e, s) =>
{
//初始化自定义视图
var customeView = new RemoteViews(this.PackageName, Resource.Layout.NotificationCustomeView); var notify = new Notification.Builder(this)
.SetTicker("自定义视图通知")
.SetSmallIcon(Resource.Drawable.Icon)
.SetNumber()
.SetContent(customeView); //设置通知的自定义视图 //设置自定义视图中textview的文字
notify.Notification.ContentView.SetTextViewText(Resource.Id.ncvTextView, "通过代码修改"); //设置自定义视图中Progressbar的进度
notify.Notification.ContentView.SetProgressBar(Resource.Id.ncvProgressBar, , , false); //给自定义视图中的ProgressBar绑定事件
var pIntent = PendingIntent.GetActivity(this, , new Intent(this, typeof(MainActivity)), );
//绑定事件
notify.Notification.ContentView.SetOnClickPendingIntent(Resource.Id.ncvProgressBar, pIntent); nMgr.Notify(, notify.Notification);
};

首先我们需要实例化一个RemoteViews封装自定义视图:

 var customeView = new RemoteViews(this.PackageName, Resource.Layout.NotificationCustomeView);

然后通过通知的SetContent将其传入

 SetContent(customeView)

下面我们还需要访问自定义视图中的控件并设置他们的值,这里我们需要使用ContentViewSetxxxxxx来设置,如下下面我们就设置了TextView的文字和ProgressBar的进度:

                 //设置自定义视图中textview的文字
notify.Notification.ContentView.SetTextViewText(Resource.Id.ncvTextView, "通过代码修改"); //设置自定义视图中Progressbar的进度
notify.Notification.ContentView.SetProgressBar(Resource.Id.ncvProgressBar, , , false);

最后就是绑定事件,通知里面的绑定事件不同于其他的,只能将一个意图与这个事件绑定(实际运用中也应该如此,比如你点击了暂停按钮,将会通过意图传递对应的参数到服务中从而停止播放

 notify.Notification.ContentView.SetOnClickPendingIntent(Resource.Id.ncvProgressBar, pIntent);

下面为实际的运行图

Xamarin.Android通知详解

至此关于通知栏的使用就结束了,本人在Galaxy S4真机上测试成功(需要设置生成的配置)。