我正在嘗試開發一個應用程序,其中我正在實現聊天功能。 但是當我收到消息時,它會發出通知聲音。 當用戶使用應用程序以編程方式時,我該如何停止通知?如何在應用程序運行時在android中以編程方式關閉通知?
private void ChatNotifications(String uid, String fuid, String message) {
NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
notiStyle.setBigContentTitle("message");
// notiStyle.setSummaryText(quote);
notiStyle.bigText(message);
Intent resultIntent = new Intent(this, Loading.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(Loading.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
//Uri defaultSoundUri1 = Uri.parse("android.resource://com.therevew.quotes/" + R.raw.hello);
Uri defaultSoundUri1= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification myNotification = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
// .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ii_launcher))
.setSound(defaultSoundUri1)
.setAutoCancel(true)
.setColor(getResources().getColor(R.color.notificationColor))
.setContentIntent(resultPendingIntent)
.setContentTitle("message")
.setContentText(message)
.setStyle(notiStyle).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(101/* ID of notification */, notiStyle.build());
}
請不要在網上或上一個問題,一些研究,SO發佈問題之前,。 – MobileEvangelist