当我的应用程序在前台时,如何关闭通知图标?

时间:2016-11-22 12:00:22

标签: android notifications android-notifications notification-icons

我面临一些问题需要你的帮助,我的应用程序在前台,当我从服务器发送通知时,通知会附带通知图标。

但我需要的是,当我的应用程序在前台用户看到通知时,通知图标不应该显示给用户。

当我的应用程序处于后台/或者应用程序未启动时,应该会显示带有通知图标的通知,并且用户按下通知图标,然后通知图标将被解除,以便正常工作。

我试过了:

if(getIntent()!=null ){
        Log.e("getIntent","getIntent");

        broadcastReciver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(NOTIFY_ACTIVITY_ACTION ))
                {
                    if(intent.getExtras().getBoolean("reload")){

                        int notificationId = intent.getIntExtra("notificationId", 0); 
                        Log.e("notificationId","notificationId---"+notificationId);
                        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                        manager.cancel(notificationId);

                        setFragmentView(0);

                        //finish();
                    }
                }
            }
        };
    }

on onMessageReceived:

Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("notificationId",NOTIFICATION_ID);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.splash_logo)
                .setContentTitle("Logizo")
                .setContentText("New Order Arrived")
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(NOTIFICATION_ID/* ID of notification */, notificationBuilder.build());

1 个答案:

答案 0 :(得分:1)

我使用以下代码删除通知图标。

 private void removeNotificationsFromStatusBar(Context context, int notificationId) {

    try {
        NotificationManager notificationManagerNS = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManagerNS.cancel(notificationId);
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(NotificationsFragment.class.getSimpleName(), "Unable to remove notification from status bar");
    }

}