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

Commit

Permalink
Huge Update
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLuo0 committed Apr 5, 2019
1 parent 0a13075 commit 9374d60
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 9 deletions.
86 changes: 86 additions & 0 deletions .idea/markdown-navigator.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified app/release/app-release.apk
Binary file not shown.
4 changes: 0 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_notification" />
</application>

</manifest>
77 changes: 75 additions & 2 deletions app/src/main/java/com/RichardLuo/notificationpush/FCMReceiver.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,82 @@
package com.RichardLuo.notificationpush;

import android.util.Log;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;

public class FCMReceiver extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
String packageName = remoteMessage.getData().get("package");
NotificationChannel mChannel;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = getSystemService(NotificationManager.class);
mChannel = new NotificationChannel(packageName, packageName, IMPORTANCE_DEFAULT);
if (!notificationManager.getNotificationChannels().contains(mChannel)) {
notificationManager.createNotificationChannel(mChannel);
}
}
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
int id = Integer.valueOf(remoteMessage.getData().get("id"));
/*switch (packageName) {
case "com.tencent.minihd.qq":
packageName = "com.tencent.mobileqq";
break;
}*/
PendingIntent intent = null;
if (isAppInstalled(packageName))
intent = PendingIntent.getActivity(this, 200, getPackageManager().getLaunchIntentForPackage(packageName), FLAG_UPDATE_CURRENT);
Notification summary = new NotificationCompat.Builder(this, packageName)
.setSmallIcon(R.drawable.ic_notification)
.setColor(getColor(R.color.colorPrimary))
.setStyle(new NotificationCompat.BigTextStyle()
.setSummaryText(packageName))
.setGroup(packageName)
.setGroupSummary(true)
.build();
notificationManagerCompat.notify(StringToA(packageName), summary);
Notification notification = new NotificationCompat.Builder(this, packageName)
.setSmallIcon(R.drawable.ic_notification)
.setColor(getColor(R.color.colorPrimary))
.setContentTitle(title)
.setContentText(body)
.setContentIntent(intent)
.setGroup(packageName)
.setAutoCancel(true)
.build();
notificationManagerCompat.notify(id, notification);
}

public boolean isAppInstalled(String packageName) {
try {
getPackageManager().getPackageInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}

public static int StringToA(String content) {
int result = 0;
int max = content.length();
for (int i = 0; i < max; i++) {
char c = content.charAt(i);
int b = (int) c;
result = result + b;
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ public void onNotificationPosted(StatusBarNotification sbn) {
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
JSONObject obj = new JSONObject();
JSONObject content = new JSONObject();
content.put("title", title + " ・ " + sbn.getPackageName());
content.put("title", title);
content.put("body", text);
content.put("package",sbn.getPackageName());
content.put("id",sbn.getId());
obj.put("to", inputID);
obj.put("notification", content);
obj.put("data", content);
String json = obj.toString();
out.write(json.getBytes());
out.flush();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.RichardLuo.notificationpush;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
Expand All @@ -24,6 +29,7 @@ public class MainActivity extends AppCompatActivity implements CompoundButton.On
Switch Swh;
EditText input;
TextView DeviceID;
Button clear;
public static String inputID;
SharedPreferences preferences;
SharedPreferences.Editor editor;
Expand All @@ -35,6 +41,19 @@ protected void onCreate(Bundle savedInstanceState) {
Swh = findViewById(R.id.switch1);
input = findViewById(R.id.editText);
DeviceID = findViewById(R.id.textView);
clear = findViewById(R.id.clear);
final NotificationManager notificationManager = getSystemService(NotificationManager.class);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
for (NotificationChannel channel : notificationManager.getNotificationChannels()
) {
notificationManager.deleteNotificationChannel(channel.getId());
}
}
}
});
Swh.setOnCheckedChangeListener(this);
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_height="wrap_content"
android:textIsSelectable="true" />

<Button
android:id="@+id/clear"
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/clear" />
</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<string name="service_name">GetNotification</string>
<string name="start">start</string>
<string name="DeviceID">DeviceID</string>
<string name="clear">remove all channels</string>
</resources>

0 comments on commit 9374d60

Please sign in to comment.