展会推送通知时,应用程序打开/以不同的方式关闭(Show push notifications wh

2019-09-02 01:38发布

在我的应用我已经从一个BaseActivity继承几个活动。
我的应用程序接收推送通知, GCMBaseIntentService
我需要实现的下一个逻辑:
当如果应用程序打开时显示的对话框,如果关闭显示通知接收推。

我的代码:

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

在我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);
    }

我总是收到通知。
当我调试context.getPackageManager().queryBroadcastReceivers(pushReceivedIntent, 0).size()总是等于0。

谁能告诉我,我做错了什么?

Answer 1:

看来,PackageManager.queryBroadcastReceivers()返回应用程序清单匹配给定的意向声明的所有接收器。

但是请注意,这将不包括与Context.registerReceiver()注册的接收器; 目前还没有办法让那些信息。

您可以使用的onReceive()下面的代码,以确定该应用程序/活动正在运行或不

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
}


Answer 2:

您可以检查应用程序是否是背景或前景使用此代码:

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

如果返回“真”,则表明通知其他显示对话框。

当我调试context.getPackageManager()。queryBroadcastReceivers(pushReceivedIntent,0).size()总是等于0。

对于这种不及格0通知(),而是通过“Calendar.getInstance()。getTimeInMillis()”价值。将展示基于时间的所有通知。

希望这会帮助你。



文章来源: Show push notifications when application open/closed in different way