NotificationService.java
5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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;
}
}