如果有例如键 update
,我不会显示从出现的顶部通知菜单收到的推送通知我的通知。现在,如果我收到带有此键的通知,所有通知都在通知栏中。我不想向用户显示此通知。
我正在使用 WakefulBroadcastReceiver
来处理如下通知:
public class PusherReceiver extends WakefulBroadcastReceiver {
private boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null)
return false;
final String packageName = context.getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
public void onReceive(final Context context, Intent intent) {
Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
if (!isAppOnForeground((context))) {
String pushNotificationBody = intent.getStringExtra("alert");
try {
JSONObject notificationData = new JSONObject(pushNotificationBody);
// This is the Intent to deliver to our service.
Intent service = new Intent(context, BackgroundService.class);
// Put here your data from the json as extra in in the intent
service.putExtra("notification", pushNotificationBody);
Log.i("PUSH_NOTIFICATION_JSON", "RECEIVED JSON " + notificationData);
// Start the service, keeping the device awake while it is launching.
if (!notificationData.has("update")) {
startWakefulService(context, service);
} else {
// Do nothing
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
更新:
我稍微改变了项目,使用 Onesignal
和他的 NotificationExtenderService
,我做了如下的事情:
public class NotificationNotDisplayingExtender extends NotificationExtenderService {
@Override
protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
String notification = receivedResult.toString();
String notificationBody = receivedResult.payload.body;
JSONObject notificationBodyJSON = null;
try {
notificationBodyJSON = new JSONObject(notificationBody);
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject pushNotificationData = notificationBodyJSON;
boolean hidden = false;
if (pushNotificationData.has("update")) {
Log.i("NOTIFICATION MANAGER", "PREVENT DISPLAY NOTIFICATION");
hidden = true;
}
// Return true to stop the notification from displaying.
return hidden;
}
}
它阻止显示带有更新 key 的通知,但现在我没有在我的 PusherReceiver 中收到它来启动我的服务。有没有简单的方法将数据从我的 NotificationNotDisplayingExtender
receivedResult 发送到我的 PusherReceiver
?
现在看来我的 PusherReceiver 没有触发他的 onReceive
方法。
非常感谢您的提前帮助。
最佳答案
有两种类型的负载。 1.数据 2.通知
https://developers.google.com/cloud-messaging/concept-options
仅使用数据负载。然后你总是在 FirebaseMessagingService 中调用 onMessageRececived 方法
关于java - 如果推送通知包含特定元素,则隐藏它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49112905/