Show push notification only when app is not running android -
in application have push notification integrated gcm
. working fine whenever notification comes appearing. push notification come when user inside app. want notifications appear when user outside app.
here code push notification:
gcmbroadcastreceiver
public class gcmbroadcastreceiver extends wakefulbroadcastreceiver { @override public void onreceive(context context, intent intent) { componentname comp = new componentname(gcmnotificationintentservice.class.getpackage().getname(), gcmnotificationintentservice.class.getname()); startwakefulservice(context, (intent.setcomponent(comp))); setresultcode(activity.result_ok); } }
gcmnotificationintentservice.class
public class gcmnotificationintentservice extends intentservice { public static int notificationid = 10; private notificationmanager mnotificationmanager; notificationcompat.builder builder; private boolean login_status = false; public gcmnotificationintentservice() { super("gcmintentservice"); } public static final string tag = gcmnotificationintentservice.class.getsimplename(); @override protected void onhandleintent(intent intent) { bundle extras = intent.getextras(); googlecloudmessaging gcm = googlecloudmessaging.getinstance(this); // getmessagetype() intent parameter must intent received // in broadcastreceiver. string messagetype = gcm.getmessagetype(intent); if ( !extras.isempty() ) { // has effect of unparcelling bundle /* * filter messages based on message type. since * gcm extended in future new message types, * ignore message types you're not interested in, or * don't recognize. */ if ( googlecloudmessaging.message_type_send_error.equals(messagetype) ) { sendnotification("send error: " + extras.tostring()); } else if ( googlecloudmessaging.message_type_deleted.equals(messagetype) ) { sendnotification("deleted messages on server: " + extras.tostring()); // if it's regular gcm message, work. } else if ( googlecloudmessaging.message_type_message.equals(messagetype) ) { sendnotification(extras.getstring("message")); log.i(tag, "received: " + extras.tostring()); } } // release wake lock provided wakefulbroadcastreceiver. gcmbroadcastreceiver.completewakefulintent(intent); } private void sendnotification(string msg) { mnotificationmanager = (notificationmanager) this.getsystemservice(context.notification_service); pendingintent contentintent = pendingintent.getactivity(this, 0, intent, pendingintent.flag_update_current); notificationcompat.builder mbuilder = new notificationcompat.builder(this).setsmallicon(r.drawable.ic_launcher) .setcontenttitle(getstring(r.string.notification_title)) .setstyle(new notificationcompat.bigtextstyle().bigtext(msg)).setcontenttext(msg); uri uri = ringtonemanager.getdefaulturi(ringtonemanager.type_notification); mbuilder.setsound(uri); mbuilder.setcontentintent(contentintent); mbuilder.setautocancel(true); mnotificationmanager.notify(notificationid++, mbuilder.build()); } }
in manifestfile
<receiver android:name=".gcmbroadcastreceiver" android:permission="com.google.android.c2dm.permission.send" > <intent-filter> <action android:name="com.google.android.c2dm.intent.receive" /> <action android:name="com.google.android.c2dm.intent.registration" /> <category android:name="com.medlife.deliveryagent" /> </intent-filter> </receiver> <service android:name=".gcmnotificationintentservice" />
any suggestions highly appreciated!!!
do check whether app in foreground or not calling below method , depending on returning value true or false rest of work
public boolean checkapp(){ activitymanager = (activitymanager) .getsystemservice(activity_service); // info running task list<activitymanager.runningtaskinfo> taskinfo = am.getrunningtasks(1); componentname componentinfo = taskinfo.get(0).topactivity; if (componentinfo.getpackagename().equalsignorecase("yourpackage")) { return true; } else { return false; } }
Comments
Post a Comment