Lolipop(5.0)以上のPush通知時におけるAutoCancelについて

257 views
Skip to first unread message

y kiyo

unread,
Mar 17, 2016, 3:10:04 AM3/17/16
to 日本Androidの会
お世話になっております。

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()にしても消えない
}
}

    /// いろいろな処理

}
}

y kiyo

unread,
Mar 25, 2016, 12:16:16 AM3/25/16
to 日本Androidの会
自己レスです。
とりあえず解決しました。

色々変更したので、どれが直接原因かがわかりませんが、
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

public class MyGcmListenerService extends GcmListenerService {

    private static final String TAG = "MyGcmListenerService";
    public static final int UNKNOWN_NOTIFICATION_ID = 999;
    private static NotificationManager mNotificationManager;

    enum NotificationType {
        NOTIFICATION_TYPE_SINGLE,
        NOTIFICATION_TYPE_MULTI,
    }

    private  NotificationType notificationType = NotificationType.NOTIFICATION_TYPE_MULTI;

    @Override
    public void onMessageReceived(String from, Bundle data) {

        String mess = data.getString("message");
        String push_type_str = data.getString("push_type");
        int push_type = 0;
        if (MCSchemeUtility.sharedInstance().isCheck(push_type_str) == true) {
            push_type = Integer.parseInt(push_type_str);
        }

        //通知バーに表示
        sendNotification(mess, push_type);

    }


    private void sendNotification(String message, int push_type) {

        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Push通知からの起動後に利用するパラメータ設定
        Bundle bundle = new Bundle();
        bundle.putString("message", message);
        bundle.putInt("push_type", push_type);

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtras(bundle);

        PendingIntent pendingIntent;
        int icon;
        int noti_id;

        if (push_type < 0) {
            push_type = UNKNOWN_NOTIFICATION_ID;
        }

        if (notificationType == NotificationType.NOTIFICATION_TYPE_SINGLE) {
            // 1種類通知モード
            noti_id = 0;
        }
        else {
            // 複数通知モード
            noti_id = push_type;
        }

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            // Lollipop未満の時
            pendingIntent = PendingIntent.getActivity(this, noti_id /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            icon = R.drawable.app_icon;
        }
        else {
            // Lollipop以上の時
            TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
            taskStackBuilder.addParentStack(MainActivity.class);
            taskStackBuilder.addNextIntent(intent);
            pendingIntent = taskStackBuilder.getPendingIntent(noti_id, PendingIntent.FLAG_UPDATE_CURRENT);

            icon = R.drawable.push_small_icon; // 白ベースアイコン
        }

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("タイトル")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setSmallIcon(icon)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        mNotificationManager.notify(noti_id /* ID of notification */, mBuilder.build());
    }
}

 


2016年3月17日木曜日 16時10分04秒 UTC+9 y kiyo:
Reply all
Reply to author
Forward
0 new messages