隐藏

在 Xamarin.Android 中使用 Notification.Builder 构建通知

发布:2021/11/8 17:51:38作者:管理员 来源:本站 浏览次数:957


0 背景


在 Android 4.0 以后,系统支持一种更先进的 Notification.Builder 类来发送通知。但 Xamarin 文档含糊其辞,多方搜索无果,遂决定自己摸索。


之前的代码:


//定义通知管理类

NoitficationManager nMgr;

nMgr = (NotificationManager)GetSystemService(NotificationService);


//设置通知的图标以及显示的简介Title

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


//初始化点击通知后打开的活动

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


//设置通知的主体

notify.SetLatestEventInfo(this, "普通通知标题", "普通通知内容", pintent);


//发送通知

nMgr.Notify(0, notify);


如果在 VS2015 ,Xamarin 4.0 版本时,使用此方法,会报错:


   “Notification.SetLatestEventInfo(Context, string, string, PendingIntent)”已过时:“deprecated”


1 Android 通知结构

一个通知的最基本元素:


   SmallIcon - 图标

   ContentTitle - 通知标题

   ContentText - 通知内容


可选元素


   Sound - 通知声音

   Priority - 通知优先级

   Ticker - 通知摘要。在 Android 5.0 以下版本,表现为在状态栏闪过的文字

   Number - 通知计数,显示为通知横幅右下角的角标数字

   还有更多,本文不再赘述


2 使用Notification.Builder 构建普通通知

下面,以在 Activity 中构建通知为例。

首先,新建项目,并声明通知管理类。


namespace HelloNotification

{

[Activity(Label = "HelloNotification", MainLauncher = true, Icon = "@drawable/icon")]

public class MainActivity : Activity

{

NotificationManager nMgr;


protected override void OnCreate(Bundle bundle)

{

base.OnCreate(bundle);

SetContentView(Resource.Layout.Main);

           //声明通知管理类

nMgr = (NotificationManager)GetSystemService(NotificationService);

}

}

}


第二步,使用 Notification.Builedr 构建一个通知。


var notify = new Notification.Builder(this)

.SetContentTitle("ContentTitle")

.SetContentText("ContentText")

.SetSmallIcon(Resource.Drawable.Icon);


发送通知时,需要用到当前 Activity 的 Context ,就是这个 this 。

第三步,发送通知!


nMgr.Notify(1, notify.Build());


值得注意的是 Notify() 方法的第一个参数 int id 。这个 id 唯一标识 Android App 里面的每个通知,也就是在同一个 App 中, id 不能重复。否则后发出的通知会覆盖之前的通知。


当然,也可以用此方法达到“覆盖通知”的目的。

3 给通知添加声音


即调用 Notification.Builder 的 SetSound 方法。这里使用默认铃声做演示,因为自定义铃声的方法。。。跟这个差很多 = =


   需要添加引用: Android.Media


var notify = new Notification.Builder(this)

   //省略了其他参数

   .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))


这样发送的通知就可以带音效了。是不是很酷炫

4 在 Android 5.0 以上发送浮动通知


这很简单,只要把通知的 Priority 设为 High 即可。

不过需要注意的是,高优先级的通知必须设置通知铃声。


var notify = new Notification.Builder(this)

   //省略了其他参数

   .SetPriority((int)NotificationPriority.High)

   .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))


5 自定义通知声音


这才是本文的灵魂所在!


调用此方法,需要把通知铃声文件放置在 Recourse 文件夹中。通常要再新建一个子文件夹(这里使用了 raw ,别问我为什么,我也不知道。也许是写起来方便吧)用来存储所有的声音文件。


目录结构:


       HelloNotification

       * Recourse

       * raw

       * AnotherRingTone.mp3

       * MyRingTone.mp3


引用外部声音文件时,需要使用文件的 Uri 。具体用法及介绍,请看代码。


//注意添加引用!!

using Uri = Android.Net.Uri;



notify = new Notification.Builder(this)

//省略的其他参数

.SetPriority((int)NotificationPriority.High)

//从 Uri 设置声音

.SetSound(Uri.Parse("android.resource://" + PackageName + "/" + Resource.Raw.MyRingTone));


可以看到,Xamarin 似乎把目录转化为了 .Net 对象,使用 . 运算符直接可以调用到,并不需要在什么奇怪的地方进行注册。


下面说一点个人理解:


SetSound 方法需要使用类型为 Android.Net.Uri 的参数,而此 Uri 只能在自己包名的目录下寻找吧 = =

所以要用 Uri.Prase 转换路径为 Uri


声明通知之后,便可以使用 nMgr.Notify(1,notify.Build()); 发送此通知了。这是便可以听见 狂拽酷炫 的自定义铃声。

6 所有代码


using System;

using Android.App;

using Android.Content;

using Android.Runtime;

using Android.Views;

using Android.Widget;

using Android.OS;

using Android.Media;


namespace HelloNotification

{

[Activity(Label = "HelloNotification", MainLauncher = true, Icon = "@drawable/icon")]

public class MainActivity : Activity

{

NotificationManager nMgr;


protected override void OnCreate(Bundle bundle)

{

base.OnCreate(bundle);

SetContentView(Resource.Layout.Main);


nMgr = (NotificationManager)GetSystemService(NotificationService);


var notify = new Notification.Builder(this)

.SetContentTitle("ContentTitle")

.SetContentText("ContentText")

.SetPriority((int)NotificationPriority.High)

.SetSound(Uri.Parse("android.resource://" + PackageName + "/" + Resource.Raw.MyRingTone));

.SetSmallIcon(Resource.Drawable.Icon);


nMgr.Notify(1, notify.Build());

}

}

}