關於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. MVC 為頁面的list資料重新命名

    function ReBookFileName() {                              $("#div_sortable").find("li[ ...

  2. 升级 DNX 和 DNVM

    升级命令: dnvm upgrade -u dnvm upgrade -u –runtime CoreCLR -u 表示 unstable(不稳定),不带 -u 表示升级到最新稳定(stable)版本 ...

  3. Java设计模式——适配器模式

    JAVA 设计模式 适配器模式 用途 适配器模式 (Adapter) 将一个类的接口转换成客户希望的另外一个接口. Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 适配器 ...

  4. Erlang 从入门到精通(一) 下载安装

    我的电脑配置: 系统:win8.1  x64 内存:16G 在官网下载http://www.erlang.org/

  5. ca des key crt scr

    openssl genrsa -des3 -out domain.key 1024 openssl req -new -key domain.key -out domain.csr openssl r ...

  6. [原创][LaTex]LaTex学习笔记入门

    0. 简介 LaTEX(/ˈlɑːtɛx/,常被读作/ˈlɑːtɛk/或/ˈleɪtɛk/),文字形式写作LaTeX,是一种基于TEX的排版系统,由美国电脑学家莱斯利·兰伯特在20世纪80年代初期开发 ...

  7. [转]VS 2013 连接数据库报错 未能加载文件或程序集 Microsoft.SqlServer.Management.Sdk.Sfc

    原文链接:http://stackoverflow.com/questions/16906686/could-not-load-file-or-assembly-microsoft-sqlserver ...

  8. 奔小康赚大钱 hdu 2255( KM )

    http://acm.split.hdu.edu.cn/showproblem.php?pid=2255 带权匹配问题: #include <stdio.h> #include <a ...

  9. 使用MySql数据库, 浏览器接收返回数据报错SyntaxError: unterminated string literal

    用php写了一个很简单的页面, 用来记录常用的oracle的关键字和各种函数, 以后用起来查一下方便, 原来是用java写了一个web项目, 但是用起来太麻烦, 真的不如php方便, 然后就把整理的内 ...

  10. win7下安装mysql

    (1)官网下载mysql: http://dev.mysql.com/downloads/mysql/ (2)解压后,进入到该目录下,将my-default.ini文件复制一份改名为my.ini 打开 ...