Show push notifications when application open/clos

2019-02-18 04:36发布

问题:

In my app I've a several Activities that inherit from one BaseActivity.
My application receive push notification with GCMBaseIntentService
I need to implement the next logic:
When push received if the application is open show dialog, if closed show notification.

My code:

    public class GCMIntentService extends GCMBaseIntentService {

----------------------- other code ----------------------------------------

    @Override
        protected void onMessage(Context context, Intent intent) {
            Log.d(TAG, "onMessage : " + String.valueOf(intent));

            // This is how to get values from the push message (data)
            String payload = intent.getExtras().getString("payload");
            String message = "";
            String messageID;

            if (payload.contains("{")) {
                try {
                    JSONObject jsonArray = new JSONObject(payload);

                    message = jsonArray.get("Msg").toString();
                    messageID = jsonArray.get("MessageID").toString();

                    GA_Handler.sendEvent("Popup_Push", String.format("Push message %s", messageID));

                } catch (Exception ex) {
                    // Do nothing
                }
            } else {
                message = payload;
            }

            // special intent with action we make up
            Intent pushReceivedIntent = new Intent(ACTION_PUSH); 
            // place old extras in new intent
            pushReceivedIntent.putExtras(intent.getExtras());
            // find out if there any BroadcastReceivers waiting for this intent
            if (context.getPackageManager().queryBroadcastReceivers(pushReceivedIntent, 0).size() > 0) {
                // We got at least 1 Receiver, send the intent
                context.sendBroadcast(pushReceivedIntent);
            } else {
                // There are no receivers, show PushNotification as Notification
                // long timestamp = intent.getLongExtra("timestamp", -1);
                NotificationManager notificationManager = (NotificationManager) context
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                Notification note = new Notification(R.drawable.ic_launcher, "MYAPP", System.currentTimeMillis());
                Intent notificationIntent = new Intent(context, SplashActivity.class);
                notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
                note.setLatestEventInfo(context, "MYAPP", message, pendingIntent);
                note.number = count++;
                note.defaults |= Notification.DEFAULT_SOUND;
                note.defaults |= Notification.DEFAULT_VIBRATE;
                note.defaults |= Notification.DEFAULT_LIGHTS;
                note.flags |= Notification.FLAG_AUTO_CANCEL;

                notificationManager.notify(0, note);
            }
        }
----------------------- other code ----------------------------------------    }

In my BaseActivity:

@Override
    protected void onResume() {
        super.onResume();

        //register as BroadcastReceiver for Push Action
        IntentFilter filter = new IntentFilter();
        filter.addAction(GCMIntentService.ACTION_PUSH);

        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
              DialogFragmentUtils.getNotification("Notification", "Notification");
            }
          };
          registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        FragmentManager fm = getSupportFragmentManager();
        for (int i = 0; i < fm.getBackStackEntryCount(); ++i) {
            fm.popBackStack();
        }

        //unregister broadcast receiver
        unregisterReceiver(mReceiver);
    }

I always receive the notifications.
When I debug the context.getPackageManager().queryBroadcastReceivers(pushReceivedIntent, 0).size() always equals to 0.

Can someone tell me what I'm doing wrong?

回答1:

It seems that PackageManager.queryBroadcastReceivers() returns all receivers declared in application manifests matching a given Intent.

Note however that this will not include receivers registered with Context.registerReceiver(); there is currently no way to get information about those.

You can use the following code in onReceive() to determine if the application/activity is running or not

ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
Log.d("current task :", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClass().getSimpleName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equalsIgnoreCase("your.package.name")){
    //Activity in foreground, broadcast intent
} 
else{
    //Activity Not Running
    //Generate Notification
}


回答2:

You can check whether application is in background or foreground using this code :

    public String isApplicationSentToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return "false";
        }
    }

    return "true";
}

If it returns "true" then show notification else show dialog box.

When I debug the context.getPackageManager().queryBroadcastReceivers(pushReceivedIntent, 0).size() always equals to 0.

For this don't pass 0 in notify().Instead pass "Calendar.getInstance().getTimeInMillis()" value.This will show all the notifications based on time.

Hope this will help you.