關於Anroid 的使用者來說,Notification 是一個非常會看到且用到的功能

他可以提醒使用者甚麼東西需要待處理,像是郵件或是會議的提醒等..

甚至有些APP ,直接使用Notification 來做記事像是 https://play.google.com/store/apps/details?id=bleetech.notificationnote

簡單且方便,這篇我們來談談如何來製作 Local Notification

首先我們來講解要實作的畫面


畫面上有三顆按鈕, 發動基本款的Notification(btn1) ,不同Id Notification(btn2),啟動Activity(btn3)

我們先來看第一個 簡單發動 Notification

 
            //同一組id 的ˊ簡單 notification 
            var btn1 = FindViewById<Button>(Resource.Id.btn1);
            btn1.Click += delegate
            {
 
 
                NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .SetContentTitle("當麻的測試通知同一組ID")
                .SetSmallIcon(Resource.Drawable.Icon2)
                .SetContentText("你點擊了" + count + "次");
 
 
                var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
                //都用同一組id
                notificationManager.Notify(0, builder.Build());
                count++;
            };

其中 notificationManager.Notify(, builder.Build());

這0 的部分就是你app 內部識別的ID 許多因為許多地方沒有說到,如果當這值相同的時候他並不會再多增加一個新的Noticication

而是會去覆蓋

結果:


像圖中,我點了12次但是依然會是用同一個Notification 他並不會去累加上去..差異點在哪我們看第二個範例

不同Id Notification(btn2):

//不同id 的ˊ簡單 notification 
var btn2 = FindViewById<Button>(Resource.Id.btn2);
btn2.Click += delegate
{
 
 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
    .SetContentTitle("當麻的測試通知不同ID")
    .SetSmallIcon(Resource.Drawable.Icon2)
    .SetContentText("你點擊了" + count + "次");
 
 
    var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
    //都用不同id count 是累加的,所以每次點擊皆不同
    notificationManager.Notify(count, builder.Build());
    count++;
};

notificationManager.Notify(count, builder.Build()); 因為count每次被點都會加一次,所以造成每次Id 皆不同

結果:


所以每一次都會去增加一個新的Notification

但是目前案例,我們發現,點擊之後並不會有任何反應

接下來,

這案例是我送出一個  Notification ,並且使用者點擊後,會啟動一個我指定名字叫做 ActivityNotiCall 的 Activity

並且我透過PendingIntent 將一個 key 為 user 值為donma的資料意圖帶入到開啟後的 ActivityNotiCall

// 會啟動Activity 的 Notification
          var btn3 = FindViewById<Button>(Resource.Id.btn3);
          btn3.Click += delegate
          {
              //成立一個新的Intent
              //並且在bundle 中帶資料
              var resultIntent = new Intent(this, typeof(ActivityNotiCall));
              var bundleData=new Bundle();
              bundleData.PutString("user","donma");
              resultIntent.PutExtras(bundleData); 
 
              TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
              stackBuilder.AddNextIntent(resultIntent);
              //建立一個PendingIntent 使用者點擊後透過TaskStackBuilder 送至新的Activity
              PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
 
              NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
              .SetContentTitle("當麻測試叫起Activity")
              .SetSmallIcon(Resource.Drawable.Icon5)
              .SetContentText("點我啟動")
                  //帶入 PendingIntent
              .SetContentIntent(resultPendingIntent);
 
 
              var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
              notificationManager.Notify(count, builder.Build());
              count++;
          };

ActivityNotiCall.cs :

using Android.App;
using Android.OS;
using Android.Widget;
 
namespace LocalNoti
{
    [Activity(Label = "My Activity")]
    public class ActivityNotiCall : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
          
 
            SetContentView(Resource.Layout.LayoutNotiCall);
            
            var txtView = (TextView)FindViewById(Resource.Id.textView1);
            txtView.Text = this.Intent.GetStringExtra("user");
        }
    }
}

結果:


點擊後


看範例都蠻好操控的,只是要多補充一點觀念,下面附上source code 還有參考連結
參考連結:

http://tw.myblog.yahoo.com/jw!IfekG5aZE0ewhGesjPZ30w--/article?mid=19&prev=20&next=18
http://developer.android.com/reference/android/app/PendingIntent.html

http://docs.xamarin.com/guides/cross-platform/application_fundamentals/notifications/android/local_notifications_in_android

http://developer.android.com/reference/android/support/v4/app/TaskStackBuilder.html

[Xamarin] 關於發出Notification 的大小事 (转帖)的更多相关文章

  1. [Xamarin] 關於Internal Storage ,存取App內部使用資料 (转帖)

    最近在開發App,會使用到必須要處理一些App所使用的資料,上網路查一下Android 得作法,包含我自己也實作了一下,可能是因為對Java || Android 不是很孰悉,常常錯在 java.la ...

  2. [Xamarin] 關於SQLite 的操作 (转帖)

    我們聊一下常用的東西,SQLite,這東西很常用到,當再寫手機APP的時候,有時候會在Client 做 cache或是做一些資料的管理都很必須會用到,我們來看看今天的範例 建立SQL Lite 資料庫 ...

  3. 開博客了, 因為搞Delphi 開發的關於Delphi學習

    開博客了, 因為搞Delphi 開發的關於Delphi學習,之前都是用本地TXT文件保存,發現在本地電腦保存非常不方面,而且只能在一台電腦上保存,不容易查看和修改內容.便於以後的記錄只用,以及經驗交流 ...

  4. 在laravel下關於blade模板的嘗試

    Blade模板 關於模板繼承和分區段 @section和@yield的實驗 ①關於@section...@show嘗試 測試1 {{--appV2test.blade.php--}} <html ...

  5. 關於Validform 控件 值得注意的地方

    Validform控件其實用起來挺方便的,直接百度就能找到官網,有直接的demo做參考.這些我就不提了,我所要說的是關於Validform控件的ajax的提交. Validform中有個參數ajaxP ...

  6. JDK1.6历史版本的下载(關於TLSv1.2)Oracle的官方文檔

    [资源描述]:对于部分老项目 仍然采用的是JDK1.6 版本 但是打开官方 JDK 都是最新的 版本 想找 历史版本 不容易找到 [资源详情]:提供下载链接: http://www.oracle.co ...

  7. [Xamarin] 調用JSON.net 來解析JSON (转帖)

    上一篇文章我們提到了透過WebClient從Facebook 拿到我的JSON資料 再來我們要怎麼解析JSON格示呢?在.net 中,我們很孰悉的JSON.net,沒錯,我們依然可以在Xamarin中 ...

  8. [Node.js] 關於 console.log 的格式化輸出

    Node.js 當中的 console.log,除了基本字串的輸出之外,還可以利用 %s.%d.%j 格式化的輸出,就讓我們來看些例子吧! 一.範例1 (字串輸出):console.js consol ...

  9. Visual Studio 跨平台開發實戰(4) - Xamarin Android 基本控制項介紹 (转帖)

    前言 不同於iOS, Xamarin 在Visual Studio中針對Android, 可以直接設計使用者介面. 在本篇教學文章中, 筆者會針對Android的專案目錄結構以及基本控制項進行介紹, ...

随机推荐

  1. DataAdapter与DataSet的使用

    1.创建数据库连接: 2.创建数据适配器(Adapter); 3.创建容器数据集(DataSet); 4.从数据集中取出指定表: 5.遍历表数据并输出: using System; using Sys ...

  2. empty isset array_key_exists 的区别

    empty: 参数为0或为NULL时(如上面列子),empty均返回TRUE,详细情况可以参见empty官方手册 isset: 参数为NULL时,返回FALSE,0与NULL在PHP中是有区别的,is ...

  3. 编程范式 epesode2 negative values, float 精度

    episode2 //it is very interesting,an excellect teacher,  I love it 1,why negative is indicated the w ...

  4. jQuery之ajax的跨域获取数据

    如果获取的数据文件存放在远程服务器上(域名不同,也就是跨域获取数据),则需要使用jsonp类型.使用这种类型的话,会创建一个查询字符串参数 callback=? ,这个参数会加在请求的URL后面.服务 ...

  5. python模块使用案例

    python模块使用案例 一.使用MySQLdb模块代码示例: # 导入 MySQLdb模块 import MySQLdb # 和服务器建立链接,host是服务器ip,我的MySQL数据库搭建在本机, ...

  6. hdu 3506 Monkey Party 区间dp + 四边形不等式优化

    http://acm.hdu.edu.cn/showproblem.php?pid=3506 四边行不等式:http://baike.baidu.com/link?url=lHOFq_58V-Qpz_ ...

  7. java实现smtp邮件发送

    一.准备工作 首先你需要已一个发送邮箱,一般的邮箱都有SMTP.POP3服务,比如QQ邮箱,登陆QQ邮箱开启SMTP服务,开启是服务器会提示你设置独立密码,这个密码是跟邮箱正常登陆的密码不同的,这个是 ...

  8. DUILIB 背景贴图

    贴图的描述 方式有两种    // 1.aaa.jpg    // 2.file='aaa.jpg' res='' restype='0' dest='0,0,0,0' source='0,0,0,0 ...

  9. UISearchBar

    UISearchBar——方便用户搜索信息 在移动应用程序的世界里,用户对信息获取的速度要求非常高!iOS用户希望他们需要的信息能够迅速地,直观地展现在他们面前 因为UITableView的上下滚动能 ...

  10. 模板(Template)

    最近阅读google chromium base container stack_container代码,深刻感觉到基础知识不扎实. // Casts the buffer in its right ...