Java 如果推送通知包含特定元素,则隐藏推送通知
我不会在顶部通知菜单my notification(我的通知)中显示接收到的推送通知,如果它具有例如键Java 如果推送通知包含特定元素,则隐藏推送通知,java,android,react-native,react-native-android,onesignal,Java,Android,React Native,React Native Android,Onesignal,我不会在顶部通知菜单my notification(我的通知)中显示接收到的推送通知,如果它具有例如键update。现在,如果我用这个键收到通知,所有通知都在通知栏中。我不想向用户显示此通知 我使用WakefulBroadcastReceiver处理通知,如下所示: public class PusherReceiver extends WakefulBroadcastReceiver { private boolean isAppOnForeground(Context context
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();
}
}
}
}
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;
}
}
它还阻止显示带有更新密钥的通知,但现在我的PusherReceiver中没有收到启动服务的通知。是否有简单的方法将数据从myNotificationNotDisplayingExtender
receivedResult发送到myPusherReceiver
目前看来,我的PusherReceiver没有启动他的onReceive
方法
非常感谢事先提供帮助。每次您
notify
显示通知时,您都会提供一个ID,用于通知以编辑或取消该通知。如果通过manager.notify(notificationId,notification)
显示通知,则可以使用manager.cancel(notificationId)
取消通知
如果要删除所有通知,可以使用
NotificationManager.cancelAll()
基本上我们有两种类型的通知
一种可以称为通知类型,即推送在发送/接收捆绑包中有一个通知
对象,当你的应用程序位于前台并且收到通知时,你必须在其中处理它。在这种情况下,如果你的应用程序在前台,那么你可以处理它,做任何你喜欢的事情,而不显示通知。但是,如果应用程序在后台,谷歌会自动创建一个通知,它会在收到的推送包中使用预定义的标题
和消息
对象来发出通知
第二种类型可以称为数据类型,在发送/接收的捆绑包中没有任何通知
对象。在这种情况下,您的应用程序处于前台或后台,您应该处理所有事情。因此,如果您将数据放在推送通知消息的data
对象中,那么一切都将掌握在您的手中
因此,简而言之,只需将数据放入通知的
data
对象中,并实现所需的逻辑。我看不到您所指的JSON数据。但是,我假设JSON中的update
键包含null
。在代码中,您正在检查JSON数据中是否包含键update
。如果该键存在于JSON正文中,则此函数将始终返回true。您可能有一个值为null
的字段,这表示您不应该在系统托盘中显示通知
在这种情况下,您可以考虑使用<代码> ISNULL> /COD>函数。如果此对象没有
update
的映射,或者其值为null
的映射,则返回true
// Start the service, keeping the device awake while it is launching.
if (!notificationData.isNull("update")) {
startWakefulService(context, service);
} else {
// Do nothing
}
是的,请使用您收到的通知中的
数据
有效负载 有两种类型的有效载荷。
1.资料
2.通知
仅使用数据有效负载。然后您总是在FirebaseMessagingService onMessageReceived方法中得到调用您应该注意到这是不推荐的,因为api 26:@user1209216是的,我知道,对于较新的api,我将对此进行更新,但现在我需要找到通知的解决方案:/如果我正确理解了您的问题,您只需停止致电NotificationManager以停止显示通知。通知仅在生成它们并调用notify时显示。只需使用您的服务处理数据,而无需构建通知。嗯,这会阻止运行唤醒服务,但仍会显示通知。重要的是,只有当应用程序处于后台/关闭状态时,此问题才会存在。谢谢Adib的建议。取消通知是可行的,但当用户收到带有振动的通知时,它看起来很奇怪,并且在收到通知后立即取消。我更新了我的问题,希望只剩下一个问题。据我所知,您修复了前一个问题,现在您有一个
广播接收器
,无法调用,您想校准它吗?如果是这种情况,您可以像这样调用您的广播接收器:Intent Intent=newintent(“my.action.string”);发送广播(意图)代码>。您需要根据所需的接收者更改意图操作。如果通知在NotificationNotDisplayingExtender
中有更新键,我通过发送有关您建议的广播来完成此操作。我创建了一个意图,并执行了如下操作,它可以工作:intent intent=new intent()代码>我设置的操作与我在清单中为我的意图设置的操作相同intent.setAction(“我的通知”)代码>之后,我将数据添加到我的意图中,并发送广播intent.putExtra(“数据”,notificationBody);发送广播(意图)代码>谢谢,它可以工作!:)@Robson这是一种方法,首先创建通知,然后删除通知。如果在实现过程中遇到任何问题,请告诉我