Android 当我的应用程序位于前台时,如何关闭通知图标?
我面临一些问题需要你的帮助,我的应用程序在前台,当我从服务器发送通知时,通知会带有通知图标 但我需要的是,当我的应用程序位于前台时,用户看到通知,则不应向用户显示通知图标 当我的应用程序处于后台/或应用程序未启动时,应发出带有通知图标的通知,用户按通知图标,然后通知图标将被解除,这样工作正常 我试过:Android 当我的应用程序位于前台时,如何关闭通知图标?,android,notifications,android-notifications,notification-icons,Android,Notifications,Android Notifications,Notification Icons,我面临一些问题需要你的帮助,我的应用程序在前台,当我从服务器发送通知时,通知会带有通知图标 但我需要的是,当我的应用程序位于前台时,用户看到通知,则不应向用户显示通知图标 当我的应用程序处于后台/或应用程序未启动时,应发出带有通知图标的通知,用户按通知图标,然后通知图标将被解除,这样工作正常 我试过: if(getIntent()!=null ){ Log.e("getIntent","getIntent"); broadcastReciver = new Br
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();
}
}
}
};
}
在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());
我使用以下代码删除通知图标
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");
}
}
我必须把这个代码放在哪里?在启动程序活动或通知服务中?您可以在任何地方使用此代码,但必须具有上下文,并通过使用上下文管理器创建通知管理器,然后通过使用notificationId.MainActivity.removeNotificationsFromStatusBar(通知ID,getApplicationContext())。。这是我从通知服务传递过来的,你到底想做什么?你不想在应用程序处于前台时显示通知吗?@Prashant是的,现在已经解决了。