NotificationService.java 5 KB
package com.example.talktalkspeak;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationService extends NotificationListenerService {
    public static final String TAG = "katextisok";
    Context context;

    public static final class ApplicationPackageNames {
        public static final String KAKAO_PACK_NAME = "com.kakao.talk";
        public static final String KAKAO_SAMPLE_PACK_NAME = "com.kakao.sdk.sample";
    }

    public static final class InterceptedNotificationCode {
        public static final int KAKAO_CODE = 2;
        public static final int KAKAO_SAMPLE_CODE = 1;
    }

    public void onCreate() {
        super.onCreate();
        this.context = getApplicationContext();
    }

    class NLServiceReceiver extends BroadcastReceiver {
        NLServiceReceiver() {
        }

        public void onReceive(Context context, Intent intent) {
            if (intent.getStringExtra("command").equals("clearall")) {
                Toast.makeText(context, "clearall", 0).show();
                NotificationService.this.cancelAllNotifications();
            } else if (intent.getStringExtra("command").equals("list")) {
                Toast.makeText(context, "list", 0).show();
                Intent i1 = new Intent("NOTIFICATION_LISTENER_SERVICE");
                i1.putExtra("notification_event", "====================");
                NotificationService.this.sendBroadcast(i1);
                int i = 1;
                for (StatusBarNotification sbn : NotificationService.this.getActiveNotifications()) {
                    Intent i2 = new Intent("NOTIFICATION_LISTENER_SERVICE");
                    i2.putExtra("notification_event", i + " " + sbn.getPackageName() + "\n");
                    NotificationService.this.sendBroadcast(i2);
                    i++;
                }
                Intent i3 = new Intent("NOTIFICATION_LISTENER_EXAMPLE");
                i3.putExtra("notificaation_event", "==== Notification List====");
                NotificationService.this.sendBroadcast(i3);
            }
        }
    }

    public IBinder onBind(Intent intent) {
        return super.onBind(intent);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void onNotificationPosted(StatusBarNotification sbn) {
        if (sbn != null) {
            int notificationCode = matchNotificationCode(sbn);
            if (notificationCode == 1) {
                Intent intent = new Intent("android.service.notification.NotificationListenerService");
                intent.putExtra("Notification Code", notificationCode);
                Bundle extras = sbn.getNotification().extras;
                String sampletitle = extras.getString(NotificationCompat.EXTRA_TITLE);
                CharSequence samplebody = extras.getCharSequence(NotificationCompat.EXTRA_TEXT);
                Log.i("NotificationListener", "샘플 푸시 제목 : " + sampletitle);
                Log.i("NotificationListener", "샘플 푸시 내용 : " + samplebody);
                intent.putExtra("sampleTitle", sampletitle);
                intent.putExtra("sampleBody", samplebody);
                sendBroadcast(intent);
            } else if (notificationCode == 2) {
                Intent intent2 = new Intent("android.service.notification.NotificationListenerService");
                intent2.putExtra("Notification Code", notificationCode);
                Bundle extras2 = sbn.getNotification().extras;
                String realTitle = extras2.getString(NotificationCompat.EXTRA_TITLE);
                CharSequence realBody = extras2.getCharSequence(NotificationCompat.EXTRA_TEXT);
                Log.i("NotificationListener", "푸시 제목 : " + realTitle);
                Log.i("NotificationListener", "푸시 내용 : " + realBody);
                if (realTitle != null && realBody != null) {
                    if (realTitle != null || realBody != null) {
                        intent2.putExtra("realTitle", realTitle);
                        intent2.putExtra("realBody", realBody);
                        sendBroadcast(intent2);
                    }
                }
            }
        }
    }

    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.i("kaTalk", "Notification Removed");
    }

    private int matchNotificationCode(StatusBarNotification sbn) {
        String packageName = sbn.getPackageName();
        if (packageName.equals(ApplicationPackageNames.KAKAO_PACK_NAME)) {
            return 2;
        }
        if (packageName.equals(ApplicationPackageNames.KAKAO_SAMPLE_PACK_NAME)) {
            return 1;
        }
        return 0;
    }
}