LoginSignup
5
7

More than 1 year has passed since last update.

Notificationをタップしたときに何らかの処理をする(初心者向け)

Last updated at Posted at 2018-04-21

基本形(Notificationを作成する基本形)

MainActivity.java
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        Notification notification = builder
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Notification Title")
                .setContentText("Notification Text")
                .setContentIntent(//[1]Notificationタップ時に実行したいPendingIntentを与える)
                .setDeleteIntent(//[2]Notification削除時に実行したいPendingIntentを与える)
                .build();

PendingIntentを[1][2]に与える方法

Activityを起動したい場合

以下のメソッドを実行することでstartActivity(intent)と同じような振る舞いをします。

PendingIntent.getActivity();

公式サイト(getActivity)
https://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context,%20int,%20android.content.Intent,%20int)

Serviceを起動したい場合

以下のメソッドを実行することでstartService(intent)と同じような振る舞いをします。

PendingIntent.getService();

公式サイト(getService)
https://developer.android.com/reference/android/app/PendingIntent.html#getService(android.content.Context,%20int,%20android.content.Intent,%20int)

Broadcastを送信したい場合

以下のメソッドを実行することでsendBroadcast(intent)と同じような振る舞いをします。

PendingIntent.getBroadcast();

公式サイト(getBroadcast)
https://developer.android.com/reference/android/app/PendingIntent.html#getBroadcast(android.content.Context,%20int,%20android.content.Intent,%20int)

参考にしたサイト

AndroidのNotificationが削除されたことを検知する
https://qiita.com/masaibar/items/e40801cd396dff9ca03b
指定時間に通知する方法
http://workpiles.com/2014/01/android-notification-alarmmanager/

5
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7