Help us understand the problem. What is going on with this article?

AndroidでNotificationをまた実装するときのメモ

More than 5 years have passed since last update.

通知を出すだけ

小さいアイコンだけを持つのが最小構成。

MainActivity.java
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());

builder.setSmallIcon(R.drawable.icon);

NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(NOTIFICATION_MINIMUM_ID, builder.build());

アイコンを設定し忘れると下のようなメッセージが出力される。

WARNING: In a future release this will crash the app: <PACKAGE_NAME>

テキスト

MainActivity.java
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.icon);

builder.setContentTitle("Title"); // 1行目
builder.setContentText("Text"); // 2行目
builder.setSubText("SubText"); // 3行目
builder.setContentInfo("Info"); // 右端
builder.setWhen(1400000000000l); // タイムスタンプ(現在時刻、メール受信時刻、カウントダウンなどに使用)

builder.setTicker("Ticker"); // 通知到着時に通知バーに表示(4.4まで)
// 5.0からは表示されない

NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(NOTIFICATION_TEXT_ID, builder.build());

img_text.jpg

カスタムレイアウト

MainActivity.java
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.icon);

RemoteViews customView = new RemoteViews(getPackageName(), R.layout.customlayout);
customView.setTextViewText(R.id.textview_text, "_(:3」 ∠)_");
builder.setContent(customView);

NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(NOTIFICATION_CUSTOMLAYOUT_ID, builder.build());

通知一覧の背景色と文字色がかぶって読めない状態を避けるために、
レイアウトの背景色と文字色を指定しておいた方がよさそう。

customlayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">

    <TextView
        android:id="@+id/textview_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="#000000"
        android:textSize="36sp" />
</RelativeLayout>

img_customlayout.jpg
(通知に見えない)

通知をタップしたときにActivityを起動

MainActivity.java
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.icon);

// 適当なIntent
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://xxx"));

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), REQUEST_CODE_TEST, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(contentIntent);

// タップで通知領域から削除する
builder.setAutoCancel(true);

NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(NOTIFICATION_INTENT_ID, builder.build());

参考

roga7zl
yahoo-japan-corp
Yahoo! JAPAN を運営しています。
https://www.yahoo.co.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした