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

Commit

Permalink
Add priority support
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLuo0 committed Apr 24, 2019
1 parent e74cd68 commit 88f64bc
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 69 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.RichardLuo.notificationpush"
minSdkVersion 22
targetSdkVersion 28
versionCode 11
versionName "1.0.9"
versionCode 12
versionName "1.1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Binary file modified app/release/app-release.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion app/release/output.json
Original file line number Diff line number Diff line change
@@ -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":{}}]
[{"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":{}}]
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Application"
android:label="@string/title_activity_application"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.RichardLuo.notificationpush.MainActivity" />
</activity>

<service
android:name=".GetNotification"
Expand Down
136 changes: 136 additions & 0 deletions app/src/main/java/com/RichardLuo/notificationpush/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.RichardLuo.notificationpush;

import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Application extends AppCompatActivity {
ListView listView;
SharedPreferences preferences;
ProgressBar progressBar;
Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(getSharedPreferences("MainActivity", MODE_PRIVATE).getInt("style", R.style.base_AppTheme_teal));
setContentView(R.layout.activity_application);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
preferences = getPreferences(MODE_PRIVATE);
listView = findViewById(R.id.listview);
progressBar = findViewById(R.id.progressBar);
final PackageManager packageManager = getPackageManager();
final List<ApplicationInfo> packageInfo = packageManager.getInstalledApplications(0);

new Thread() {
@Override
public void run() {
super.run();
final List<info> packageView = new ArrayList<>();
for (final ApplicationInfo applicationInfo : packageInfo) {
final String name = packageManager.getApplicationLabel(applicationInfo).toString();
Spinner.OnItemSelectedListener onItemSelectedListener = new Spinner.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
preferences.edit().remove(applicationInfo.packageName).apply();
return;
}
preferences.edit().putInt(applicationInfo.packageName, position).apply();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
};
packageView.add(new info(name, packageManager.getApplicationIcon(applicationInfo), onItemSelectedListener,preferences.getInt(applicationInfo.packageName, 0)));
}

handler.post(new Runnable() {
@Override
public void run() {
listView.setAdapter(new BaseAdapter() {
@Override
public int getCount() {
return packageView.size();
}

@Override
public Object getItem(int position) {
return packageView.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
info info = packageView.get(position);
if (convertView == null) {
convertView = LayoutInflater.from(getBaseContext()).inflate(R.layout.app_layout, listView, false);
holder = new ViewHolder();
holder.text = convertView.findViewById(R.id.appName);
holder.icon = convertView.findViewById(R.id.imageView);
holder.spinner = convertView.findViewById(R.id.spinner);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(info.text);
holder.icon.setImageDrawable(info.icon);
holder.spinner.setSelection(info.selection);
holder.spinner.setOnItemSelectedListener(info.onItemSelectedListener);
return convertView;
}
});
progressBar.setVisibility(View.GONE);
}
});
}

class ViewHolder {
TextView text;
ImageView icon;
Spinner spinner;
}

class info {
String text;
Drawable icon;
Spinner.OnItemSelectedListener onItemSelectedListener;
int selection;

info(String text, Drawable icon, Spinner.OnItemSelectedListener onItemSelectedListener, int selection) {
this.text = text;
this.icon = icon;
this.onItemSelectedListener = onItemSelectedListener;
this.selection = selection;
}
}
}.start();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

switch (getSharedPreferences("Application", MODE_PRIVATE).getInt(packageName, 0)) {
case 0:
priority = "normal";
break;
case 1:
priority = "high";
break;
case 2:
return;
}

//此处对单个应用进行单独定义
switch (packageName) {
case "com.RichardLuo.notificationpush":
Expand All @@ -54,16 +65,18 @@ public void onNotificationPosted(StatusBarNotification sbn) {
case "com.tencent.mobileqq":
if (!(title.contains("QQ空间"))) {
if (oneNotification.tickerText != null) {
String[] tickerText = oneNotification.tickerText.toString().replaceAll("\n", " ").split(":", 2);
if (tickerText[0].charAt(tickerText[0].length() - 1) == ')') {
String[] name_group = tickerText[0].split("\\(", 2);
senderName = name_group[0];
title = name_group[1].substring(0, name_group[1].length() - 1);
String tickerText = oneNotification.tickerText.toString().replace("\n", "");
Matcher matcher = Pattern.compile("^(.*?)\\((((?![()]).)*?)\\):(.*?)$").matcher(tickerText);
if (matcher.find()) {
senderName = matcher.group(1);
title = matcher.group(2);
body = matcher.group(4);
} else {
senderName = tickerText[0];
title = tickerText[0];
String[] single = tickerText.split(":", 2);
senderName = single[0];
title = single[0];
body = single[1];
}
body = tickerText[1];
} else {
if ((body.contains("联系人给你") || body.contains("你收到了")) && title.equals("QQ"))
return;
Expand All @@ -80,21 +93,13 @@ public void onNotificationPosted(StatusBarNotification sbn) {
}
}

switch (getSharedPreferences("MainActivity", MODE_PRIVATE).getInt("priority", 0)) {
case 0:
priority = "normal";
break;
case 1:
priority = "high";
break;
}

HttpURLConnection connection;
try {
URL url = new URL("https://fcm.googleapis.com/fcm/send");
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setConnectTimeout(1000);
connection.setReadTimeout(1000);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "key=" + Authorization);
Expand All @@ -118,9 +123,7 @@ public void onNotificationPosted(StatusBarNotification sbn) {
out.close();
connection.getResponseCode();
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
Expand Down
37 changes: 19 additions & 18 deletions app/src/main/java/com/RichardLuo/notificationpush/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
Expand All @@ -32,14 +31,15 @@

import java.util.List;

//TODO 增加应用前台判断
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
static SharedPreferences preferences;
static Intent service;
boolean isEnabled;
Switch Swh;
Spinner spinner;
Switch swh;
EditText input;
TextView DeviceID;
Button priority;
Button clear;
Button colors;
Button about;
Expand All @@ -52,18 +52,13 @@ protected void onCreate(Bundle savedInstanceState) {
preferences = getPreferences(MODE_PRIVATE);
setTheme(preferences.getInt("style", R.style.base_AppTheme_teal));
setContentView(R.layout.activity_main);
Swh = findViewById(R.id.switch1);
spinner = findViewById(R.id.spinner);
spinner.setSelection(preferences.getInt("priority", 0));
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
preferences.edit().putInt("priority", position).apply();
}
swh = findViewById(R.id.switch1);

priority = findViewById(R.id.priority);
priority.setOnClickListener(new Button.OnClickListener() {
@Override
public void onNothingSelected(AdapterView<?> parent) {

public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), Application.class));
}
});

Expand Down Expand Up @@ -151,7 +146,7 @@ public void onClick(DialogInterface dialog, int which) {
normalDialog.show();
}
});
Swh.setOnCheckedChangeListener(this);
swh.setOnCheckedChangeListener(this);
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
Expand Down Expand Up @@ -187,9 +182,10 @@ protected void onStart() {
super.onStart();
if (isNotificationListenersEnabled()) {
isEnabled = true;
Swh.setChecked(true);
swh.setChecked(true);
} else {
isEnabled = false;
swh.setChecked(false);
}
preferences.edit().putString("ID", input.getText().toString()).apply();
}
Expand All @@ -202,18 +198,23 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!input.getText().toString().trim().isEmpty()) {
if (isEnabled) {
preferences.edit().putString("ID", input.getText().toString()).apply();
input.setEnabled(false);
startService(service = new Intent(this, GetNotification.class));
} else {
startActivity(intent);
Swh.setChecked(false);
input.setEnabled(true);
swh.setChecked(false);
}
} else {
Toast.makeText(this, "请填写设备ID", Toast.LENGTH_SHORT).show();
preferences.edit().putString("ID", input.getText().toString()).apply();
Swh.setChecked(false);
input.setEnabled(true);
swh.setChecked(false);
}
} else {
startActivity(intent);
if (isEnabled)
startActivity(intent);
input.setEnabled(true);
stopService(service);
}
}
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/res/layout/activity_application.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />


<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />

</FrameLayout>
Loading

0 comments on commit 88f64bc

Please sign in to comment.