Skip to content

Instantly share code, notes, and snippets.

@iampaul83
Created April 22, 2015 06:21
Show Gist options
  • Save iampaul83/3494a5e7a05646454135 to your computer and use it in GitHub Desktop.
Save iampaul83/3494a5e7a05646454135 to your computer and use it in GitHub Desktop.
[Android - Wear] 第二章 - Notification基礎篇
---
tags: Android, Wear, Notification
---
>
[TOC]
***
對Android Wear來說,Notification算是一個很重要的功能之一,
可以快速且明確地提供使用者資訊,當手持設備(手機或平板),與手錶連線時,
手持設備會自動分享Notification到手錶之上。
![Android Wear](https://lh3.googleusercontent.com/-LitLSlkf4Ao/VTck9EJ068I/AAAAAAAALcE/QqJXQZ__ljs/s0/notification_phone@2x+2.png "notification_phone@2x 2.png")
# 1.一般的Notification
在手機端,傳送一般的Nofication,手錶也會同時收到。
一般的Nofication需要以下幾個步驟
1. 決定辨識的ID
2. 建立Notifiation
3. 透過NotificationManagerCompat發送出去
```
private void sendNotification()
{
int notificationId = 001;
//決定這個notification的辨識編號
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("內容");
//利用NotificationCompat.Builder來建立其內容
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
//取得NotificationManagerCompat的實體
notificationManager.notify(notificationId, notificationBuilder.build());
//填入辨識編號及notification後,即可發送出去
}
```
**注意**:建立Notifiation時,一定要設定Icon,否則就不會傳送出去。
***
>手機及手錶收到通知時的畫面
>
>![手機及手錶收到通知時的畫面](https://lh3.googleusercontent.com/-C_WKMgbWV3U/VTcwaWwSZcI/AAAAAAAALcY/Jkm6CCzcOyk/s0/001.png "001.png")
***
>手錶打開通知的畫面
>
![手錶打開通知的畫面](https://lh3.googleusercontent.com/--u6g2-bH59A/VTcxTXXyC1I/AAAAAAAALck/OBEFt1TUqW4/s0/002.png "002.png")
***
# 2.加入Intent
有時候可能會想讓使用者點了通知後開啟某個頁面。
```
private void sendNotification()
{
int notificationId = 001;
//決定這個notification的辨識編號
Intent intent = new Intent(this , MainActivity.class);
//建立Intent , 並決定要開啟哪個class
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, intent, 0);
//使用PendingIntent來等待執行這個intent
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("內容")
.setContentIntent(pendingIntent);
//設定ContentIntent為剛才建立的pendingIntent
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
//取得NotificationManagerCompat的實體
notificationManager.notify(notificationId, notificationBuilder.build());
//填入辨識編號及notification後,即可發送出去
}
```
**注意**:這邊的Intent要使用**PendingIntent**才行。
如此一來,我們的通知會多一個頁面,點下去後就會開啟剛才指定的Class
![開啟剛才指定的Class](https://lh3.googleusercontent.com/-qZwN09GsxeQ/VTc2Rj4LPoI/AAAAAAAALdo/Txkvd8UzcLg/s0/003.png "003.png")
***
#3.加入Action Button
也許我們會想要有更多的頁面來做其他的事情,像是除了Open on Phone以外,
還想開啟其他頁面,或者是開啟Google Map之類的。
```
private void sendNotification()
{
int notificationId = 001;
//決定這個notification的辨識編號
Intent intent = new Intent(this , MainActivity.class);
//建立Intent , 並決定要開啟哪個class
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, intent, 0);
//使用PendingIntent來等待執行這個intent
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
//建立Intent , 這次改ACTION_VIEW
Uri geoUri = Uri.parse("geo:0,0?q=" + Uri.encode("台北火車站"));
//建立一個Uri,假設要找台北火車站。
mapIntent.setData(geoUri);
//設定資料
PendingIntent mapPendingIntent =
PendingIntent.getActivity(this, 0, mapIntent, 0);
//使用PendingIntent來等待執行這個intent
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("內容")
.setContentIntent(pendingIntent)
.addAction(R.drawable.ic_launcher , "Map" , mapPendingIntent);
//使用addACtion來加入這個頁面,並填入圖片,說明,及要執行的PendingIntent
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
//取得NotificationManagerCompat的實體
notificationManager.notify(notificationId, notificationBuilder.build());
//填入辨識編號及notification後,即可發送出去
}
```
這樣一來你的通知又會多一個頁面,點下去後就會開啟Google Map,並到台北火車站。
如果模擬器的Google Map有問題,記得上Google Play更新。
![Google Map](https://lh3.googleusercontent.com/-RphjvSndJX0/VTc1qavjt3I/AAAAAAAALdY/oTe3P5WLgLY/s0/004.png "004.png")
***
#4.建立只有Wear才有的頁面
上述的例子都是手錶與手機共通都有訊息。
也許有些情況,我們希望特定的頁面只存在手錶端。
就必須靠WearableExtender才能達成。
```
private void sendNotification()
{
int notificationId = 001;
//決定這個notification的辨識編號
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
//建立Intent , 這次改ACTION_VIEW
Uri geoUri = Uri.parse("geo:0,0?q=" + Uri.encode("台北火車站"));
//建立一個Uri,假設要找台北火車站。
mapIntent.setData(geoUri);
//設定資料
PendingIntent mapPendingIntent =
PendingIntent.getActivity(this, 0, mapIntent, 0);
//使用PendingIntent來等待執行這個intent
NotificationCompat.Action action = new NotificationCompat.Action.Builder
(R.drawable.ic_launcher , "Map" , mapPendingIntent).build();
//建立一個NotificationCompat.Action物件,透過Builder來建立,並填入圖片,說明及PendingIntent
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("內容")
.extend(new NotificationCompat.WearableExtender().addAction(action));
//使用extend,new一個WearableExtender,並加入剛才建立的action
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
//取得NotificationManagerCompat的實體
notificationManager.notify(notificationId, notificationBuilder.build());
//填入辨識編號及notification後,即可發送出去
}
```
如此一來,地圖的頁面就只會在手錶端出現了。
![只會在手錶端出現](https://lh3.googleusercontent.com/-iWuqZ4E5PzM/VTc1J7-S9nI/AAAAAAAALdE/FsSQhghw4Gc/s0/005.png "005.png")
***
#5.增加背景圖片及修改icon
透過setLargeIcon即可讓你的Notification有背景圖片。
```
private void sendNotification()
{
int notificationId = 001;
//決定這個notification的辨識編號
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
//建立Intent , 這次改ACTION_VIEW
Uri geoUri = Uri.parse("geo:0,0?q=" + Uri.encode("台北火車站"));
//建立一個Uri,假設要找台北火車站。
mapIntent.setData(geoUri);
//設定資料
PendingIntent mapPendingIntent =
PendingIntent.getActivity(this, 0, mapIntent, 0);
//使用PendingIntent來等待執行這個intent
NotificationCompat.Action action = new NotificationCompat.Action.Builder
(R.drawable.ic_launcher , "Map" , mapPendingIntent).build();
//建立一個NotificationCompat.Action物件,透過Builder來建立,並填入圖片,說明及PendingIntent
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.pika)
.setLargeIcon(BitmapFactory.decodeResource(
getResources(), R.drawable.nbg))
.setContentTitle("Title")
.setContentText("內容")
.extend(new NotificationCompat.WearableExtender().addAction(action));
//使用setLargeIcon來設定背景圖片
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
//取得NotificationManagerCompat的實體
notificationManager.notify(notificationId, notificationBuilder.build());
//填入辨識編號及notification後,即可發送出去
}
```
你可能會碰到這個問題,明明背景圖片改了,icon也改,但是手錶還是小機器人。
>![手錶還是小機器人](https://lh3.googleusercontent.com/-i95gWPjdEA0/VTc0TOk2ScI/AAAAAAAALc4/4SfpMHA3tz0/s0/006.png "006.png")
>因為手錶的Notification的icon是對應你APP的icon,因此要改的話要改APP的icon,
>而不是單純的只改seticon,seticon只能在手機端上更改。
>將AndroidManifest裡面的android:icon改成你要的icon即可。
***
> Written with [StackEdit](https://stackedit.io/).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment