Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 88f64bc

Browse files
committed
Add priority support
1 parent e74cd68 commit 88f64bc

12 files changed

Lines changed: 267 additions & 69 deletions

File tree

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId "com.RichardLuo.notificationpush"
88
minSdkVersion 22
99
targetSdkVersion 28
10-
versionCode 11
11-
versionName "1.0.9"
10+
versionCode 12
11+
versionName "1.1.0"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {

app/release/app-release.apk

3.89 KB
Binary file not shown.

app/release/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":11,"versionName":"1.0.9","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
1+
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":12,"versionName":"1.1.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,18 @@
1515
<intent-filter>
1616
<action android:name="android.intent.action.MAIN" />
1717
<action android:name="android.intent.action.VIEW" />
18+
1819
<category android:name="android.intent.category.LAUNCHER" />
1920
</intent-filter>
2021
</activity>
22+
<activity
23+
android:name=".Application"
24+
android:label="@string/title_activity_application"
25+
android:parentActivityName=".MainActivity">
26+
<meta-data
27+
android:name="android.support.PARENT_ACTIVITY"
28+
android:value="com.RichardLuo.notificationpush.MainActivity" />
29+
</activity>
2130

2231
<service
2332
android:name=".GetNotification"
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.RichardLuo.notificationpush;
2+
3+
import android.content.SharedPreferences;
4+
import android.content.pm.ApplicationInfo;
5+
import android.content.pm.PackageManager;
6+
import android.graphics.drawable.Drawable;
7+
import android.os.Bundle;
8+
import android.os.Handler;
9+
import android.support.v7.app.AppCompatActivity;
10+
import android.view.LayoutInflater;
11+
import android.view.View;
12+
import android.view.ViewGroup;
13+
import android.widget.AdapterView;
14+
import android.widget.BaseAdapter;
15+
import android.widget.ImageView;
16+
import android.widget.ListView;
17+
import android.widget.ProgressBar;
18+
import android.widget.Spinner;
19+
import android.widget.TextView;
20+
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.Objects;
24+
25+
public class Application extends AppCompatActivity {
26+
ListView listView;
27+
SharedPreferences preferences;
28+
ProgressBar progressBar;
29+
Handler handler = new Handler();
30+
31+
@Override
32+
protected void onCreate(Bundle savedInstanceState) {
33+
super.onCreate(savedInstanceState);
34+
setTheme(getSharedPreferences("MainActivity", MODE_PRIVATE).getInt("style", R.style.base_AppTheme_teal));
35+
setContentView(R.layout.activity_application);
36+
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
37+
preferences = getPreferences(MODE_PRIVATE);
38+
listView = findViewById(R.id.listview);
39+
progressBar = findViewById(R.id.progressBar);
40+
final PackageManager packageManager = getPackageManager();
41+
final List<ApplicationInfo> packageInfo = packageManager.getInstalledApplications(0);
42+
43+
new Thread() {
44+
@Override
45+
public void run() {
46+
super.run();
47+
final List<info> packageView = new ArrayList<>();
48+
for (final ApplicationInfo applicationInfo : packageInfo) {
49+
final String name = packageManager.getApplicationLabel(applicationInfo).toString();
50+
Spinner.OnItemSelectedListener onItemSelectedListener = new Spinner.OnItemSelectedListener() {
51+
@Override
52+
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
53+
if (position == 0) {
54+
preferences.edit().remove(applicationInfo.packageName).apply();
55+
return;
56+
}
57+
preferences.edit().putInt(applicationInfo.packageName, position).apply();
58+
}
59+
60+
@Override
61+
public void onNothingSelected(AdapterView<?> parent) {
62+
63+
}
64+
};
65+
packageView.add(new info(name, packageManager.getApplicationIcon(applicationInfo), onItemSelectedListener,preferences.getInt(applicationInfo.packageName, 0)));
66+
}
67+
68+
handler.post(new Runnable() {
69+
@Override
70+
public void run() {
71+
listView.setAdapter(new BaseAdapter() {
72+
@Override
73+
public int getCount() {
74+
return packageView.size();
75+
}
76+
77+
@Override
78+
public Object getItem(int position) {
79+
return packageView.get(position);
80+
}
81+
82+
@Override
83+
public long getItemId(int position) {
84+
return position;
85+
}
86+
87+
@Override
88+
public View getView(int position, View convertView, ViewGroup parent) {
89+
ViewHolder holder;
90+
info info = packageView.get(position);
91+
if (convertView == null) {
92+
convertView = LayoutInflater.from(getBaseContext()).inflate(R.layout.app_layout, listView, false);
93+
holder = new ViewHolder();
94+
holder.text = convertView.findViewById(R.id.appName);
95+
holder.icon = convertView.findViewById(R.id.imageView);
96+
holder.spinner = convertView.findViewById(R.id.spinner);
97+
convertView.setTag(holder);
98+
} else {
99+
holder = (ViewHolder) convertView.getTag();
100+
}
101+
holder.text.setText(info.text);
102+
holder.icon.setImageDrawable(info.icon);
103+
holder.spinner.setSelection(info.selection);
104+
holder.spinner.setOnItemSelectedListener(info.onItemSelectedListener);
105+
return convertView;
106+
}
107+
});
108+
progressBar.setVisibility(View.GONE);
109+
}
110+
});
111+
}
112+
113+
class ViewHolder {
114+
TextView text;
115+
ImageView icon;
116+
Spinner spinner;
117+
}
118+
119+
class info {
120+
String text;
121+
Drawable icon;
122+
Spinner.OnItemSelectedListener onItemSelectedListener;
123+
int selection;
124+
125+
info(String text, Drawable icon, Spinner.OnItemSelectedListener onItemSelectedListener, int selection) {
126+
this.text = text;
127+
this.icon = icon;
128+
this.onItemSelectedListener = onItemSelectedListener;
129+
this.selection = selection;
130+
}
131+
}
132+
}.start();
133+
}
134+
135+
136+
}

app/src/main/java/com/RichardLuo/notificationpush/GetNotification.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
import android.service.notification.NotificationListenerService;
77
import android.service.notification.StatusBarNotification;
88

9-
import org.json.JSONException;
109
import org.json.JSONObject;
1110

1211
import java.io.DataOutputStream;
13-
import java.io.IOException;
1412
import java.net.HttpURLConnection;
1513
import java.net.URL;
14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
1616

1717
public class GetNotification extends NotificationListenerService {
1818
protected final String Authorization = "";
@@ -43,6 +43,17 @@ public void onNotificationPosted(StatusBarNotification sbn) {
4343
if (title.contains("正在运行") || title.contains("running")) return;
4444
if (inputID == null) return;
4545

46+
switch (getSharedPreferences("Application", MODE_PRIVATE).getInt(packageName, 0)) {
47+
case 0:
48+
priority = "normal";
49+
break;
50+
case 1:
51+
priority = "high";
52+
break;
53+
case 2:
54+
return;
55+
}
56+
4657
//此处对单个应用进行单独定义
4758
switch (packageName) {
4859
case "com.RichardLuo.notificationpush":
@@ -54,16 +65,18 @@ public void onNotificationPosted(StatusBarNotification sbn) {
5465
case "com.tencent.mobileqq":
5566
if (!(title.contains("QQ空间"))) {
5667
if (oneNotification.tickerText != null) {
57-
String[] tickerText = oneNotification.tickerText.toString().replaceAll("\n", " ").split(":", 2);
58-
if (tickerText[0].charAt(tickerText[0].length() - 1) == ')') {
59-
String[] name_group = tickerText[0].split("\\(", 2);
60-
senderName = name_group[0];
61-
title = name_group[1].substring(0, name_group[1].length() - 1);
68+
String tickerText = oneNotification.tickerText.toString().replace("\n", "");
69+
Matcher matcher = Pattern.compile("^(.*?)\\((((?![()]).)*?)\\):(.*?)$").matcher(tickerText);
70+
if (matcher.find()) {
71+
senderName = matcher.group(1);
72+
title = matcher.group(2);
73+
body = matcher.group(4);
6274
} else {
63-
senderName = tickerText[0];
64-
title = tickerText[0];
75+
String[] single = tickerText.split(":", 2);
76+
senderName = single[0];
77+
title = single[0];
78+
body = single[1];
6579
}
66-
body = tickerText[1];
6780
} else {
6881
if ((body.contains("联系人给你") || body.contains("你收到了")) && title.equals("QQ"))
6982
return;
@@ -80,21 +93,13 @@ public void onNotificationPosted(StatusBarNotification sbn) {
8093
}
8194
}
8295

83-
switch (getSharedPreferences("MainActivity", MODE_PRIVATE).getInt("priority", 0)) {
84-
case 0:
85-
priority = "normal";
86-
break;
87-
case 1:
88-
priority = "high";
89-
break;
90-
}
91-
9296
HttpURLConnection connection;
9397
try {
9498
URL url = new URL("https://fcm.googleapis.com/fcm/send");
9599
connection = (HttpURLConnection) url.openConnection();
96100
connection.setDoOutput(true);
97101
connection.setConnectTimeout(1000);
102+
connection.setReadTimeout(1000);
98103
connection.setRequestMethod("POST");
99104
connection.setRequestProperty("Content-Type", "application/json");
100105
connection.setRequestProperty("Authorization", "key=" + Authorization);
@@ -118,9 +123,7 @@ public void onNotificationPosted(StatusBarNotification sbn) {
118123
out.close();
119124
connection.getResponseCode();
120125
connection.disconnect();
121-
} catch (IOException e) {
122-
e.printStackTrace();
123-
} catch (JSONException e) {
126+
} catch (Exception e) {
124127
e.printStackTrace();
125128
}
126129
}

app/src/main/java/com/RichardLuo/notificationpush/MainActivity.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import android.support.v7.app.AppCompatActivity;
1717
import android.text.TextUtils;
1818
import android.view.View;
19-
import android.widget.AdapterView;
2019
import android.widget.Button;
2120
import android.widget.CompoundButton;
2221
import android.widget.EditText;
@@ -32,14 +31,15 @@
3231

3332
import java.util.List;
3433

34+
//TODO 增加应用前台判断
3535
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
3636
static SharedPreferences preferences;
3737
static Intent service;
3838
boolean isEnabled;
39-
Switch Swh;
40-
Spinner spinner;
39+
Switch swh;
4140
EditText input;
4241
TextView DeviceID;
42+
Button priority;
4343
Button clear;
4444
Button colors;
4545
Button about;
@@ -52,18 +52,13 @@ protected void onCreate(Bundle savedInstanceState) {
5252
preferences = getPreferences(MODE_PRIVATE);
5353
setTheme(preferences.getInt("style", R.style.base_AppTheme_teal));
5454
setContentView(R.layout.activity_main);
55-
Swh = findViewById(R.id.switch1);
56-
spinner = findViewById(R.id.spinner);
57-
spinner.setSelection(preferences.getInt("priority", 0));
58-
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
59-
@Override
60-
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
61-
preferences.edit().putInt("priority", position).apply();
62-
}
55+
swh = findViewById(R.id.switch1);
6356

57+
priority = findViewById(R.id.priority);
58+
priority.setOnClickListener(new Button.OnClickListener() {
6459
@Override
65-
public void onNothingSelected(AdapterView<?> parent) {
66-
60+
public void onClick(View v) {
61+
startActivity(new Intent(getApplicationContext(), Application.class));
6762
}
6863
});
6964

@@ -151,7 +146,7 @@ public void onClick(DialogInterface dialog, int which) {
151146
normalDialog.show();
152147
}
153148
});
154-
Swh.setOnCheckedChangeListener(this);
149+
swh.setOnCheckedChangeListener(this);
155150
FirebaseInstanceId.getInstance().getInstanceId()
156151
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
157152
@Override
@@ -187,9 +182,10 @@ protected void onStart() {
187182
super.onStart();
188183
if (isNotificationListenersEnabled()) {
189184
isEnabled = true;
190-
Swh.setChecked(true);
185+
swh.setChecked(true);
191186
} else {
192187
isEnabled = false;
188+
swh.setChecked(false);
193189
}
194190
preferences.edit().putString("ID", input.getText().toString()).apply();
195191
}
@@ -202,18 +198,23 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
202198
if (!input.getText().toString().trim().isEmpty()) {
203199
if (isEnabled) {
204200
preferences.edit().putString("ID", input.getText().toString()).apply();
201+
input.setEnabled(false);
205202
startService(service = new Intent(this, GetNotification.class));
206203
} else {
207204
startActivity(intent);
208-
Swh.setChecked(false);
205+
input.setEnabled(true);
206+
swh.setChecked(false);
209207
}
210208
} else {
211209
Toast.makeText(this, "请填写设备ID", Toast.LENGTH_SHORT).show();
212210
preferences.edit().putString("ID", input.getText().toString()).apply();
213-
Swh.setChecked(false);
211+
input.setEnabled(true);
212+
swh.setChecked(false);
214213
}
215214
} else {
216-
startActivity(intent);
215+
if (isEnabled)
216+
startActivity(intent);
217+
input.setEnabled(true);
217218
stopService(service);
218219
}
219220
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<ListView
7+
android:id="@+id/listview"
8+
android:layout_width="match_parent"
9+
android:layout_height="wrap_content" />
10+
11+
12+
<ProgressBar
13+
android:id="@+id/progressBar"
14+
style="?android:attr/progressBarStyle"
15+
android:layout_width="wrap_content"
16+
android:layout_height="wrap_content"
17+
android:layout_gravity="center" />
18+
19+
</FrameLayout>

0 commit comments

Comments
 (0)