我只想在通知到来且应用程序已打开时显示对话框。 如果应用程序像 divyabhaskar Android 应用程序一样打开,我不想在通知栏上显示通知
最佳答案
当通知来的时候写下面的代码
ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am
.getRunningTasks(1);
Log.d("current task :", "CURRENT Activity ::"
+ taskInfo.get(0).topActivity.getClass().getSimpleName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
className = componentInfo.getClassName();
Log.v("", "class Name-" + className);
if (extras != null) {
if (componentInfo.getPackageName().equalsIgnoreCase(
getPackageName())) {
Log.v("", "inside app-");
Intent broadcast = new Intent(ApplicationConstants.MY_BROADCAST);
broadcast.putExtra(ApplicationConstants.BOOKING_ID, bookingId);
broadcast.putExtra(ApplicationConstants.NOTIFICATION_TYPE, type);
broadcast.putExtra(ApplicationConstants.MESSAGE, message);
broadcast.putExtra(ApplicationConstants.CLASS_NAME, className);
LocalBroadcastManager.getInstance(this).sendBroadcast(
broadcast);
Log.v("", "Sending broadcast....");
} else {
sendNotification(extras);
}
并在你想要对话的 Activity 中注册广播接收器。
LocalBroadcastManager.getInstance(this).registerReceiver(receiver,
new IntentFilter(ApplicationConstants.MY_BROADCAST));
接收广播
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ApplicationConstants.MY_BROADCAST)) {
//Log.v("", "inside on receiver-");
showDialog(intent.getStringExtra(ApplicationConstants.MESSAGE));
}
}
};
在onStop方法中注销广播
@Override
protected void onStop() {
super.onStop();
if (receiver != null) {
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
}
}
关于android - 当应用程序已经打开并且通知来了时,如何在对话框中显示推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36905343/