android - Show push notifications when application open/closed in different way -


in app i've several activities inherit 1 baseactivity.
application receive push notification gcmbaseintentservice
need implement next logic:
when push received if application 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));              // how values 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) {                     // nothing                 }             } else {                 message = payload;             }              // special intent action make             intent pushreceivedintent = new intent(action_push);              // place old extras in new intent             pushreceivedintent.putextras(intent.getextras());             // find out if there broadcastreceivers waiting intent             if (context.getpackagemanager().querybroadcastreceivers(pushreceivedintent, 0).size() > 0) {                 // got @ least 1 receiver, send intent                 context.sendbroadcast(pushreceivedintent);             } else {                 // there no receivers, show pushnotification 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 baseactivity:

@override     protected void onresume() {         super.onresume();          //register broadcastreceiver 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();         (int = 0; < fm.getbackstackentrycount(); ++i) {             fm.popbackstack();         }          //unregister broadcast receiver         unregisterreceiver(mreceiver);     } 

i receive notifications.
when debug context.getpackagemanager().querybroadcastreceivers(pushreceivedintent, 0).size() equals 0.

can tell me i'm doing wrong?

it seems packagemanager.querybroadcastreceivers() returns receivers declared in application manifests matching given intent.

note not include receivers registered context.registerreceiver(); there no way information those.

you can use following code in onreceive() determine if application/activity running or not

activitymanager = (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 } 

Comments