日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2023/12/19 Android 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 xamarin android 通知,在 Xamarin.Android 中使用 Notification.Builder 构建通知 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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;

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

}

}

}

總結

以上是生活随笔為你收集整理的xamarin android 通知,在 Xamarin.Android 中使用 Notification.Builder 构建通知的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。