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

Commit

Permalink
Small improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLuo0 committed Apr 12, 2019
1 parent 8ddd41c commit 765b949
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
applicationId "com.RichardLuo.notificationpush"
minSdkVersion 22
targetSdkVersion 28
versionCode 1
versionCode 4
versionName "1.0.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
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":1,"versionName":"1.0.5","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":4,"versionName":"1.0.5","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
42 changes: 18 additions & 24 deletions app/src/main/java/com/RichardLuo/notificationpush/FCMReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@

public class FCMReceiver extends FirebaseMessagingService {
static Map<String, PendingIntent> Package_Intent = new HashMap<>();
static String installedQQ = null;
final static String[] QQNames = new String[]{"com.tencent.mobileqq", "com.tencent.tim", "com.tencent.mobileqqi", "com.tencent.qqlite", "com.tencent.minihd.qq"};

int color = 0;
NotificationManagerCompat notificationManagerCompat;

Expand All @@ -50,24 +49,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
case "com.tencent.qqlite":
case "com.tencent.tim":
case "com.tencent.mobileqq":
if (installedQQ == null) {
if (!(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && getSystemService(NotificationManager.class).getNotificationChannels().contains(packageName))) {
List<PackageInfo> info = this.getPackageManager().getInstalledPackages(0);
for (int i = 0; i < info.size(); i++) {
String ipackage = info.get(i).packageName;
for (String QQName : QQNames) {
if (QQName.equals(ipackage)) {
installedQQ = QQName;
break;
}
}
if (installedQQ != null)
break;
}
} else
installedQQ = packageName;
}
packageName = installedQQ;
packageName = getSharedPreferences("MainActivity", MODE_PRIVATE).getString("installedQQ", null);
forQQ(packageName, title, body, getIntent(packageName), notificationManagerCompat);
return;
}
Expand Down Expand Up @@ -125,9 +107,22 @@ private static int StringToA(String content) {
return result;
}

private void setChannel(String packageName) {
private boolean isChannelExist(String packageName) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
List<NotificationChannel> channels = getSystemService(NotificationManager.class).getNotificationChannels();
for (NotificationChannel channel : channels) {
if (channel.getId().equals(packageName)) {
return true;
}
}
return false;
}
return false;
}

public void setChannel(String packageName) {
NotificationChannel mChannel;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !getSystemService(NotificationManager.class).getNotificationChannels().contains(packageName)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !isChannelExist(packageName)) {
mChannel = new NotificationChannel(packageName, packageName, IMPORTANCE_DEFAULT);
getSystemService(NotificationManager.class).createNotificationChannel(mChannel);
}
Expand Down Expand Up @@ -159,8 +154,7 @@ private Notification getCurrentNotification(String packageName, int id) {
return null;
}

private void forQQ(String packageName, String title, String body, PendingIntent
intent, NotificationManagerCompat notificationManagerCompat) {
private void forQQ(String packageName, String title, String body, PendingIntent intent, NotificationManagerCompat notificationManagerCompat) {
setChannel(packageName);
Notification notification;
if (!(body.contains("联系人给你") || title.contains("QQ空间") || body.contains("你收到了"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -27,6 +28,8 @@
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;

import java.util.List;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
static String inputID;
static SharedPreferences preferences;
Expand All @@ -38,6 +41,8 @@ public class MainActivity extends AppCompatActivity implements CompoundButton.On
Button colors;
Button about;

final static String[] QQNames = new String[]{"com.tencent.mobileqq", "com.tencent.tim", "com.tencent.mobileqqi", "com.tencent.qqlite", "com.tencent.minihd.qq"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -141,6 +146,19 @@ public void onComplete(@NonNull Task<InstanceIdResult> task) {
});
startService(new Intent(this, FCMReceiver.class));
input.setText(preferences.getString("ID", ""));

if (preferences.getString("installedQQ", null) == null) {
List<PackageInfo> info = this.getPackageManager().getInstalledPackages(0);
for (int i = 0; i < info.size(); i++) {
String ipackage = info.get(i).packageName;
for (String QQName : QQNames) {
if (QQName.equals(ipackage)) {
preferences.edit().putString("installedQQ", QQName).apply();
break;
}
}
}
}
}

@Override
Expand Down

0 comments on commit 765b949

Please sign in to comment.