first commit
This commit is contained in:
commit
4140922530
10 files changed
+2362
No files matched your search
@@ -0,0 +1,196 @@
|
||||
package com.bluememe.utils;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.firebase.messaging.FirebaseMessagingService;
|
||||
import com.google.firebase.messaging.RemoteMessage;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class BluememeMessagingService extends FirebaseMessagingService {
|
||||
private static final String TAG = "BluememeMessaging";
|
||||
private static final String CHANNEL_ID = "bluememe_utils_notifications";
|
||||
private static final String EXTRA_PAYLOAD = "bluememe_payload";
|
||||
|
||||
@Override
|
||||
public void onNewToken(String token) {
|
||||
super.onNewToken(token);
|
||||
BluememeUtils.saveToken(this, token);
|
||||
try {
|
||||
JSONObject event = new JSONObject();
|
||||
event.put("type", "token");
|
||||
event.put("token", token);
|
||||
BluememeUtils.publishEvent(this, event);
|
||||
} catch (JSONException ex) {
|
||||
Log.w(TAG, "Unable to publish token event.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(RemoteMessage remoteMessage) {
|
||||
super.onMessageReceived(remoteMessage);
|
||||
JSONObject notification = remoteMessageToJson(remoteMessage);
|
||||
try {
|
||||
JSONObject event = new JSONObject();
|
||||
event.put("type", "notification");
|
||||
event.put("notification", notification);
|
||||
BluememeUtils.publishEvent(this, event);
|
||||
} catch (JSONException ex) {
|
||||
Log.w(TAG, "Unable to publish notification event.", ex);
|
||||
}
|
||||
|
||||
if (!isAppInForeground() && shouldShowNotification(notification)) {
|
||||
showNotification(notification);
|
||||
}
|
||||
}
|
||||
|
||||
private JSONObject remoteMessageToJson(RemoteMessage message) {
|
||||
JSONObject json = new JSONObject();
|
||||
try {
|
||||
json.put("messageId", message.getMessageId());
|
||||
json.put("from", message.getFrom());
|
||||
json.put("collapseKey", message.getCollapseKey());
|
||||
json.put("sentTime", message.getSentTime());
|
||||
|
||||
JSONObject data = new JSONObject();
|
||||
for (Map.Entry<String, String> entry : message.getData().entrySet()) {
|
||||
data.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
json.put("data", data);
|
||||
|
||||
RemoteMessage.Notification remoteNotification = message.getNotification();
|
||||
if (remoteNotification != null) {
|
||||
JSONObject notification = new JSONObject();
|
||||
notification.put("title", remoteNotification.getTitle());
|
||||
notification.put("body", remoteNotification.getBody());
|
||||
notification.put("tag", remoteNotification.getTag());
|
||||
notification.put("clickAction", remoteNotification.getClickAction());
|
||||
notification.put("channelId", remoteNotification.getChannelId());
|
||||
if (remoteNotification.getImageUrl() != null) {
|
||||
notification.put("imageUrl", remoteNotification.getImageUrl().toString());
|
||||
}
|
||||
json.put("notification", notification);
|
||||
}
|
||||
} catch (JSONException ex) {
|
||||
Log.w(TAG, "Unable to convert RemoteMessage to JSON.", ex);
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
private boolean shouldShowNotification(JSONObject payload) {
|
||||
JSONObject notification = payload.optJSONObject("notification");
|
||||
if (notification != null) {
|
||||
return !TextUtils.isEmpty(notification.optString("title", ""))
|
||||
|| !TextUtils.isEmpty(notification.optString("body", ""));
|
||||
}
|
||||
JSONObject data = payload.optJSONObject("data");
|
||||
return data != null
|
||||
&& (!TextUtils.isEmpty(data.optString("title", ""))
|
||||
|| !TextUtils.isEmpty(data.optString("body", "")));
|
||||
}
|
||||
|
||||
private void showNotification(JSONObject payload) {
|
||||
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
if (notificationManager == null) {
|
||||
return;
|
||||
}
|
||||
createNotificationChannel(notificationManager);
|
||||
|
||||
String title = valueFromPayload(payload, "title", appLabel());
|
||||
String body = valueFromPayload(payload, "body", "");
|
||||
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(getPackageName());
|
||||
if (launchIntent == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
launchIntent.putExtra(EXTRA_PAYLOAD, payload.toString());
|
||||
|
||||
int flags = PendingIntent.FLAG_UPDATE_CURRENT;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
flags |= PendingIntent.FLAG_IMMUTABLE;
|
||||
}
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(this, payload.toString().hashCode(), launchIntent, flags);
|
||||
|
||||
int icon = getApplicationInfo().icon == 0 ? android.R.drawable.ic_dialog_info : getApplicationInfo().icon;
|
||||
Notification.Builder builder = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
|
||||
? new Notification.Builder(this, CHANNEL_ID)
|
||||
: new Notification.Builder(this);
|
||||
|
||||
builder.setSmallIcon(icon)
|
||||
.setContentTitle(title)
|
||||
.setContentText(body)
|
||||
.setAutoCancel(true)
|
||||
.setContentIntent(pendingIntent)
|
||||
.setShowWhen(true);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
||||
builder.setStyle(new Notification.BigTextStyle().bigText(body));
|
||||
}
|
||||
|
||||
notificationManager.notify(payload.toString().hashCode(), builder.build());
|
||||
}
|
||||
|
||||
private String valueFromPayload(JSONObject payload, String key, String defaultValue) {
|
||||
JSONObject notification = payload.optJSONObject("notification");
|
||||
if (notification != null && !TextUtils.isEmpty(notification.optString(key, ""))) {
|
||||
return notification.optString(key);
|
||||
}
|
||||
JSONObject data = payload.optJSONObject("data");
|
||||
if (data != null && !TextUtils.isEmpty(data.optString(key, ""))) {
|
||||
return data.optString(key);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
private String appLabel() {
|
||||
ApplicationInfo applicationInfo = getApplicationInfo();
|
||||
CharSequence label = getPackageManager().getApplicationLabel(applicationInfo);
|
||||
return label == null ? getPackageName() : label.toString();
|
||||
}
|
||||
|
||||
private void createNotificationChannel(NotificationManager notificationManager) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
||||
return;
|
||||
}
|
||||
NotificationChannel channel = new NotificationChannel(
|
||||
CHANNEL_ID,
|
||||
"Application notifications",
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
);
|
||||
notificationManager.createNotificationChannel(channel);
|
||||
}
|
||||
|
||||
private boolean isAppInForeground() {
|
||||
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
|
||||
if (activityManager == null) {
|
||||
return false;
|
||||
}
|
||||
List<ActivityManager.RunningAppProcessInfo> processes = activityManager.getRunningAppProcesses();
|
||||
if (processes == null) {
|
||||
return false;
|
||||
}
|
||||
String packageName = getPackageName();
|
||||
for (ActivityManager.RunningAppProcessInfo process : processes) {
|
||||
if (process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
|
||||
&& packageName.equals(process.processName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user