gpt4 book ai didi

android - 当应用程序已经打开并且通知来了时,如何在对话框中显示推送通知?

转载 作者:行者123 更新时间:2023-11-29 01:20:49 25 4
gpt4 key购买 nike

我只想在通知到来且应用程序已打开时显示对话框。如果应用程序像 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com