[Xamarin] 關於發出Notification 的大小事

2013-07-12

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

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

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

2013-07-12_133144

簡單且方便,這篇我們來談談如何來製作 Local Notification
首先我們來講解要實作的畫面
2013-07-12_133357

畫面上有三顆按鈕, 發動基本款的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(0, builder.Build());
這0 的部分就是你app 內部識別的ID 許多因為許多地方沒有說到,如果當這值相同的時候他並不會再多增加一個新的Noticication
而是會去覆蓋
結果:
Screenshot_2013-07-12-13-29-15

像圖中,我點了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 皆不同
結果:
Screenshot_2013-07-12-13-29-22

所以每一次都會去增加一個新的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");
        }
    }
}

結果:
Screenshot_2013-07-12-13-29-29

點擊後
Screenshot_2013-07-12-13-29-33

看範例都蠻好操控的,只是要多補充一點觀念,下面附上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

source code:


當麻許的超技八 2014 | Donma Hsu Design.