Skip to content

Instantly share code, notes, and snippets.

@yukpiz
Created February 17, 2014 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukpiz/c4e70429e692767113e0 to your computer and use it in GitHub Desktop.
Save yukpiz/c4e70429e692767113e0 to your computer and use it in GitHub Desktop.

Android notification.

  • 起動しているアプリケーションから、端末のステータスバーに通知を出す
  • Notificationを使った方法は非推奨である
  • Notification.Builderを利用する
  • API Level 11(Android3.0)以降の場合は、Notification.Builderを利用
  • API Level 4(Android1.6)以降の場合は、NotificationCompact.Builderを利用

Notificationの表示

  • MainActivity.java
package notification.pop;

//import android.support.v4.app.NotificationCompact;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;

- (省略)


//PendingIntentの発行
/*
Intentはアプリケーションの目的を
   Activity, Service, Broadcastに伝える為に使われる
PnedingIntentとは
   あるタイミングで発行されるIntentを発行する為に使われる
あるタイミングとは
   時間を指定して発行
   イベントの発生時に発行
?: PendingIntentを使う必要性。

第4引数のFLAGは、PendingIntentが発行された時、
一度用意してあるPendingIntentの扱いを指定するもの?
FLAG_CANCEL_CURRENT 新しく作るけど前のはキャンセルするべき
FLAG_NO_CREATE 新しく作らない?つくろうとする?
FLAG_ONE_SHOT 一度発行されていれば再発行しない
FLAG_UPDATE_CURRENT 既にあればそれを使いまわす?
*/
PendingIntent pIntent = PendingIntent.getActivity(
   this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);

//NotificationManagerの定義
NotificationManager manager =
   (NotificationManager) this.getSystemService(Service.NOTIFICATION_SERVICE);

//Builderの定義
Notification.Builder builder = new Notification.Builder(this);
builder.setContentIntent(pIntent);
//ステータスバーに表示されるアイコン
builder.setSmallIcon(R.drawable.ic_small);
//ステータスバーの展開時に表示されるアイコン
builder.setLargeIcon(R.drawble.ic_large);
builder.setAutoCancel(true);
//ステータスバーに表示される文字列
builder.setTicker("pop notice!!");
//ステータスバーの展開時に表示されるタイトル文字列
builder.setContentTitle("content title.");
//ステータスバーの展開時に表示される詳細文字列
builder.setContentText("content text.");

//通知をPOP
manager.notify(0, builder.build());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment