Show Push Notifications When Application Open/closed In Different Way
Solution 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
}
Solution 2:
You can check whether application is in background or foreground using this code :
public String isApplicationSentToBackground(final Context context) {
ActivityManageram= (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentNametopActivity= 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.
Post a Comment for "Show Push Notifications When Application Open/closed In Different Way"