24
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Android(4.4)の通知内容を取得したい。

Last updated at Posted at 2016-02-05

#目的

通知のイベントを監視して、通知内容をアプリで利用したい。

#セキュリティ・通知アクセス
アプリから通知内容を取得するには、セキュリティ → 通知アクセス →、アプリにチェックを付ける必要がある。プログラムからその画面を起動することも出来る。

 private void showNotificationAccessSettingMenu() {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
        startActivity(intent);
 }

自分のアプリが通知内容を取得することが可能なのか確認

private boolean isEnabledReadNotification() {
        ContentResolver contentResolver = getContentResolver();
        String rawListeners = Settings.Secure.getString(contentResolver,
                "enabled_notification_listeners");
        if (rawListeners == null || "".equals(rawListeners)) {
            return false;
        } else {
            String[] listeners = rawListeners.split(":");
            for (String listener : listeners) {
                if (listener.startsWith(getPackageName())) {
                    return true;
                }
            }
        }
        return false;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (!isEnabledReadNotification()) {
        showNotificationAccessSettingMenu();
    } else {
        
    }

NotificationListenerService( Android4.3 ~ )

NotificationListenerServiceを継承すれば、通知のイベントを監視して、通知内容を取得することが可能。


import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

import java.util.ArrayList;

public class NotificationService extends NotificationListenerService {

    private String TAG = "Notification";
    
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {    
        //通知が更新
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        //通知が削除
    }
}

##サービスなので宣言が必要

AndroidManifest.xml

   <service android:name=".NotificationService"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name=
                    "android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>

getActiveNotifications

通知に表示されている内容をすべて取得する。


@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    StatusBarNotification[] notifications = getActiveNotifications();
    for (StatusBarNotification object: notifications){
            String package_name = object.getPackageName();
            String post_date = new SimpleDateFormat("yyyy/MM/dd").format(object.getPostTime());
            CharSequence ticket = object.getNotification().tickerText;
            String message;

            if (ticket != null) {
                 message = ticket.toString();
            } else {
                 message = "なし";
            }

            Log.d(TAG, package_name + ":" + post_date + ":" + message);

    }
}

24
22
0

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
24
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?