This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a13075
commit 9374d60
Showing
8 changed files
with
193 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 75 additions & 2 deletions
77
app/src/main/java/com/RichardLuo/notificationpush/FCMReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters