質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

2回答

4552閲覧

Android Lolipop(5.0)以上の時のNotificationのAutoCancelについて

ykiyota

総合スコア21

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2016/03/18 07:17

お世話になっております。

Push通知を受信した後、通知センターからのタップでアプリ起動の後、通知センターから該当通知を削除したいのですが、
Lolipop(5.0)以上だとNotificationのAutoCancelが動かずに困っております。

また、AutoCancelが効かなくても個別に削除できればと思い、
MainActivity側で削除しようとしたのですが削除できませんでした。

どのようにすれば通知の削除ができるのでしょうか?
情報をお持ちの方がいらっしゃいましたら、ご教示いただけますと有難いです。
よろしくお願いいたします。

以下、環境及びGCMIntentServiceとMainActivityの一部抜粋したものです。

環境:
Android Studio 1.5.1
buildToolsVersion "23.0.2"

■■■GCMIntentService

public class GcmIntentService extends IntentService {
private static final String TAG = "GcmIntentService";
private NotificationManager mNotificationManager;

public GcmIntentService() { super("GcmIntentService"); } @Override protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras(); GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); String messageType = gcm.getMessageType(intent); if (!extras.isEmpty()) { if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { Log.d(TAG,"messageType: " + messageType + ",body:" + extras.toString()); } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { Log.d(TAG,"messageType: " + messageType + ",body:" + extras.toString()); } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { Log.d(TAG,"messageType: " + messageType + ",body:" + extras.toString()); String mess = extras.getString("message"); String push_type_str = extras.getString("type"); int push_type = 0; push_type = Integer.parseInt(push_type_str); //通知バーに表示 sendNotification(mess, push_type); } } GcmBroadcastReceiver.completeWakefulIntent(intent); } private void sendNotification(String msg, int push_type) { mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE); Intent main_intent = new Intent(this, MainActivity.class); // Push通知からの起動後に利用するパラメータ設定 Bundle bundle = new Bundle(); bundle.putString("message", msg); bundle.putInt("push_type", push_type); main_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_CLEAR_TOP); main_intent.putExtras(bundle); PendingIntent contentIntent; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { contentIntent = PendingIntent.getActivity(this, push_type, main_intent, PendingIntent.FLAG_CANCEL_CURRENT); } else { TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this); taskStackBuilder.addNextIntent(main_intent); contentIntent = taskStackBuilder.getPendingIntent(push_type, PendingIntent.FLAG_CANCEL_CURRENT); } int notification_id = push_type; NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setWhen(System.currentTimeMillis()) .setContentTitle("title") .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setContentText(msg); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { mBuilder.setSmallIcon(R.drawable.app_icon); } else { mBuilder.setSmallIcon(R.drawable.push_small_icon); // 白ベース画像 } mBuilder.setContentIntent(contentIntent); mBuilder.setAutoCancel(true); // push_typeは3種類のタイプがあり、push_typeによりPush起動後の処理を変更する。 // push_type毎にpush通知を受けられるようにする mNotificationManager.notify(notification_id, mBuilder.build()); }

}

■■■ MainActivity

public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private NotificationManager mNotificationManager;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); Bundle bundle = getIntent().getExtras(); if (bundle == null) { int flag = getIntent().getFlags(); if ((flag & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { finish(); return; } } else { NotificationManager mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE); int push_type = bundle.getInt("push_type", -1); if (push_type != -1) { mNotificationManager.cancel(push_type); // ★★★通知が消えない!!!★★★ ※cancelAll()にしても消えない } } /// いろいろな処理 }

}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Odacchi

2016/03/18 09:21 編集

ソースコードが見づらいため、下記のように囲っていただけないでしょうか? ```Java (ここにコードを書く) ```
guest

回答2

0

自己レスです。
とりあえず解決しました。

色々変更したので、どれが直接原因かがわかりませんが、
5.0になってからAutoCancelの処理を利かすためのタイミングが厳しくなったような印象です。
以下にやったことと、一部ソースの記載します。

・GCM2.0→GCM3.0
・SDK 23.2.0 →23.2.1
・スプラッシュ表示処理変更
修正前:MainActivityを非同期で1秒後に閉じて、DetailActivityをStartActivityする。 ←この処理があると通知が消えなかった…
修正後:Pushを受けた直後はBundleをそのままDetailActivityに渡してMainActivityはそのままFinish、DetailActivityの最初の処理でSplash表示を行なう。

■■■ MyGcmListenerService.java

Java

1 2public class MyGcmListenerService extends GcmListenerService { 3 4 private static final String TAG = "MyGcmListenerService"; 5 public static final int UNKNOWN_NOTIFICATION_ID = 999; 6 private static NotificationManager mNotificationManager; 7 8 enum NotificationType { 9 NOTIFICATION_TYPE_SINGLE, 10 NOTIFICATION_TYPE_MULTI, 11 } 12 13 private NotificationType notificationType = NotificationType.NOTIFICATION_TYPE_MULTI; 14 15 @Override 16 public void onMessageReceived(String from, Bundle data) { 17 18 String mess = data.getString("message"); 19 String push_type_str = data.getString("push_type"); 20 int push_type = 0; 21 if (MCSchemeUtility.sharedInstance().isCheck(push_type_str) == true) { 22 push_type = Integer.parseInt(push_type_str); 23 } 24 25 //通知バーに表示 26 sendNotification(mess, push_type); 27 28 } 29 30 31 private void sendNotification(String message, int push_type) { 32 33 mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 34 35 // Push通知からの起動後に利用するパラメータ設定 36 Bundle bundle = new Bundle(); 37 bundle.putString("message", message); 38 bundle.putInt("push_type", push_type); 39 40 Intent intent = new Intent(this, MainActivity.class); 41 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 42 intent.putExtras(bundle); 43 44 PendingIntent pendingIntent; 45 int icon; 46 int noti_id; 47 48 if (push_type < 0) { 49 push_type = UNKNOWN_NOTIFICATION_ID; 50 } 51 52 if (notificationType == NotificationType.NOTIFICATION_TYPE_SINGLE) { 53 // 1種類通知モード 54 noti_id = 0; 55 } 56 else { 57 // 複数通知モード 58 noti_id = push_type; 59 } 60 61 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { 62 // Lollipop未満の時 63 pendingIntent = PendingIntent.getActivity(this, noti_id /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT); 64 65 icon = R.drawable.app_icon; 66 } 67 else { 68 // Lollipop以上の時 69 TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this); 70 taskStackBuilder.addParentStack(MainActivity.class); 71 taskStackBuilder.addNextIntent(intent); 72 pendingIntent = taskStackBuilder.getPendingIntent(noti_id, PendingIntent.FLAG_UPDATE_CURRENT); 73 74 icon = R.drawable.push_small_icon; // 白ベースアイコン 75 } 76 77 Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 78 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 79 .setContentTitle("タイトル") 80 .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) 81 .setSmallIcon(icon) 82 .setAutoCancel(true) 83 .setSound(defaultSoundUri) 84 .setContentIntent(pendingIntent); 85 86 mNotificationManager.notify(noti_id /* ID of notification */, mBuilder.build()); 87 } 88} 89

投稿2016/03/25 04:20

ykiyota

総合スコア21

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

AndroidのNotificationは、5.0以降大きく変わっていて、修正が大変だった記憶がたしかにあります。
ただ、1年以上前の記憶なので、即答ができませんが、参考になれば幸いです。

英語ですが、こちらに全く同様の現象が報告されていますね。
5.0.2で、正常に動作確認ができているソースコードも上がっているので、ヒントになるかもしれません。

また、Android5.0のNotificationの機能変更について、日本語のまとめ記事を見つけました。
参考までに。

投稿2016/03/18 09:35

Odacchi

総合スコア907

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ykiyota

2016/03/18 10:28

ご回答ありがとうございます。 紹介していただいたソースコードを元に修正してみましたが状況は変わらず… AutoCancelの設定自体が無いので、setAutoCancel(true)も付加してみましたが、 それでもダメでした…
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問