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

标签 android push-notification

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

相关文章:

java - 从 HTTP 响应中获取 JSON 对象

android - 我可以在应用关闭时更改推送通知的声音吗?

ios - 如果应用程序强制退出,则处理 IOS 远程通知

php - 将数据从服务器推送到用户浏览器

android - 未连接。调用 Connect 或等待 onConnected() 被调用

java - 更改不在应用程序类中的书法默认字体

ios - 我正在使用 firebase 处理推送通知。我的 "didReceiveRemoteNotification"函数没有被调用

带有空 aps 字典的 iOS 推送通知

安卓浏览器漏洞? div溢出滚动

android - TextView编程中如何设置撇号