Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide push notification if it contains specific elements

I won't show a received push notification from appearing top notifications menu my notification if it has for example key update. For now if I get notification with this key, all notifications are in the notification bar. I want to not present this notifications for user.

I'm using WakefulBroadcastReceiver for handle notifications like below:

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();
            }
        }
    }
}

UPDATE:

I changed project a little and with Onesignal and his NotificationExtenderService, I did something like below:

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;
    }
}

And it prevent displaying notifications with update key, but now I don't receive it in my PusherReceiver to start my service. Is there easy way to send data from my NotificationNotDisplayingExtender receivedResult to my PusherReceiver?

For now it looks like my PusherReceiver don't fire his onReceive method.

Many thanks for help in advance.

like image 832
Robson Avatar asked Mar 05 '18 14:03

Robson


People also ask

Can push notifications be personalized?

To add an extra layer of personalization to an iOS or Android push campaign message, you can use geo-location based push notifications. There are two ways brands can send push notifications based on a user's location: Location-based notifications: App users receive these pushes based on data collected on the backend.

What is opt in push notification?

iOS and Web push notifications are based on a push opt-in model that doesn't allow for apps or websites to send push notifications unless the user agrees beforehand. On Android platforms, users are quick to disable notifications that they don't want.

Are push notifications encrypted?

Push content is sent and stored as plain text, unencrypted, and therefore can potentially be read at any point in between.


2 Answers

There are two types of payload. 1. Data 2. Notification

https://developers.google.com/cloud-messaging/concept-options

Use only data payload. Then you always get the call in FirebaseMessagingService onMessageRececived Method

like image 180
KKSINGLA Avatar answered Sep 23 '22 03:09

KKSINGLA


The thing is basically we have two type of notifications.

One which can be called Notification Type, is that the push has a notification object in sent/received bundle, in which you have to handle it when your app is in foreground and the notification is received. In this case, if your app is in foreground, then you can handle it and do whatever you like which is not showing a notification. But if the app is in background, a notification will automatically create by google and it takes predefined title and message objects within the received push bundle to make the notification.

Second type which can be called Data Type, do not have any notification object in the sent/received bundle. In this scenario, your app is in foreground or background, you should handle everything. So, if you put your data in data object of your push notification message, everything will be in your hands.

So, in short, just put your data in data object of your notification and implement your desired logic.

like image 25
A. Badakhshan Avatar answered Sep 23 '22 03:09

A. Badakhshan