我面临一些问题需要您的帮助,我将我的应用程序放在前景,当我从服务器发送通知时,通知随附通知图标。
但是我需要的是,当我的应用在前景用户看到通知时,不应向用户显示通知图标。
当我的应用在后台/或未启动应用程序时,请使用通知图标的通知来临,然后用户按Notification Icon,然后通知图标被视为完美工作。
我尝试过:
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());
i通过使用以下代码删除通知图标。
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");
}
}