I am using this to show notifications:
public static void showAlarmNotification(Context context, Class<?> cls, String title, String content, int idNotification){
Intent notificationIntent = new Intent(context, cls);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(cls);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(idNotification, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID).setPriority(NotificationCompat.PRIORITY_MAX);
Notification notification = builder.setContentTitle(title)
.setContentText(content)
.setAutoCancel(false)
.setOngoing(true)
.setVibrate(new long[] {1000, 1000, 1000})
.setSmallIcon(R.drawable.ic_stat_name)
.setChannelId(NOTIFICATION_CHANNEL_ID)
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID_ALARM, context.getString(R.string.alarms), NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(null,null);
notificationChannel.setVibrationPattern(new long[] {1000, 1000, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(idNotification, notification);
}
It works fine to display a notification. But I want the notification not to disappear automatically as it normally happens, I want it to only go up if the user swipes it up, I have seen applications doing that but I have not found a way to do it
UPDATE
This example shows a notification that is not hidden until the user swipes it up
Example (YouTube)
That appears to be a notification with a set category of CATEGORY_ALARM.
See also system-wide category
The solution is to change setContentIntent to setFullScreenIntent, this is the source
Related
I have implement Push notification firebase services class and add on my project and send notification API Request through so notification proper receive but notification sound work to app active condition only but it will be require all notification receive time.
any one this type condition face and resolve please suggest changes.
String title = Objects.requireNonNull(remoteMessage.getNotification()).getTitle();
String text = remoteMessage.getNotification().getBody();
remoteMessage.getMessageType();
// String channelId = remoteMessage.getNotification().getChannelId();
// Log.d("channelId", "onMessageReceived: "+channelId);
final String CHANNEL_ID = "HEADS_UP_NOTIFICATION";
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
"Heads Up notification",
NotificationManager.IMPORTANCE_HIGH
);
getSystemService(NotificationManager.class).createNotificationChannel(channel);
Notification.Builder notification = new Notification.Builder(this,CHANNEL_ID)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.demo)
.setAutoCancel(true)
.setVibrate(new long[] { 0,100,300,500,800,1000, 1000, 1000, 1000, 1000 })
.setOngoing(true)
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_SOUND)
.setVisibility(Notification.VISIBILITY_PUBLIC);
// custom notification
/* MediaPlayer mp;
mp = MediaPlayer.create(PushNotificationServices.this, R.raw.short_notification_sound);
mp.start();*/
#SuppressLint("UnspecifiedImmutableFlag") PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
notification.setContentIntent(contentIntent);
NotificationManagerCompat.from(this).notify(1,notification.build());
super.onMessageReceived(remoteMessage);
Try to use setSound method instead of setDefaults.
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notification: Notification.Builder = Builder(this, CHANNEL_ID)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.demo)
.setAutoCancel(true)
.setVibrate(longArrayOf(0, 100, 300, 500, 800, 1000, 1000, 1000, 1000, 1000))
.setOngoing(true)
.setPriority(Notification.PRIORITY_MAX)
--> .setSound(defaultSoundUri)
.setVisibility(Notification.VISIBILITY_PUBLIC)
First You have uninstall your app then try again.
If this does not work for then try using below code
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = CommonUtill.random();
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.logo)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody).setBigContentTitle(getString(R.string.app_name)))
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel mChannel = new NotificationChannel(channelId,
getString(R.string.app_name),
NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
mChannel.setDescription("desc");
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setSound(sound, attributes); // This is IMPORTANT
if (notificationManager != null)
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(943/* ID of notification */, notificationBuilder.build());
This is reference link
Android Push Notification Sound is missing (Not Working ) background and foreground on android
in my application, i have created 3 notification channels (Follow, Comment, Like) to receive notifications from Firebase Cloud Messaging.
When i run app and go to Settings > Apps > My App > App Notifications , I found it show only one notification channel (ex. Follow channel) and other two channels are't be shown ...
But when i receive notification from Comment channel for example, it updates current channel with Comment channel and when i receive notification from Like channel, it updates current channel with Like channel and so on..
so why it doesn't show three channels as separated channels at same time ?!
Here is Code I work with:
public class MyFireBaseMessagingService extends FirebaseMessagingService {
String title, content, type;
#Override
public void onMessageReceived(#NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
title = remoteMessage.getData().get("Notif_Title");
content= remoteMessage.getData().get("Notif_Content");
type = remoteMessage.getData().get("Type");
if (type.equals("Follow")) {
NotificationManager manager1 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent result = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, result, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(postContent)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent)
//to work on android 8 and high..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("YOUR_PACKAGE_NAME");
builder.setSmallIcon(R.drawable.logo);
NotificationChannel channel = new NotificationChannel(
"YOUR_PACKAGE_NAME",
"Follow",
NotificationManager.IMPORTANCE_HIGH);
manager1.createNotificationChannel(channel);
}
manager1.notify(0, builder.build());
} else if (type.equals("Comment")) {
NotificationManager manager2 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent result = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 1, result, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(content)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent)
//to work on android 8 and high..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("YOUR_PACKAGE_NAME");
builder.setSmallIcon(R.drawable.logo);
NotificationChannel channel = new NotificationChannel(
"YOUR_PACKAGE_NAME",
"Comment",
NotificationManager.IMPORTANCE_HIGH);
manager3.createNotificationChannel(channel);
}
manager3.notify(1, builder.build());
} else if (type.equals("Like")) {
Intent result = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 2, result, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager manager3 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title))
.setContentText(content)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent)
//to work on android 8 and high..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("YOUR_PACKAGE_NAME");
builder.setSmallIcon(R.drawable.logo);
NotificationChannel channel = new NotificationChannel(
"YOUR_PACKAGE_NAME",
"Like",
NotificationManager.IMPORTANCE_HIGH);
manager3.createNotificationChannel(channel);
}
manager3.notify(2, builder.build());
}
}
}
Can Anyone help me...
I know how to solve it ...
You should change this code
builder.setChannelId("YOUR_PACKAGE_NAME");
builder.setSmallIcon(R.drawable.logo);
Into specific channel id to each one like :
builder.setChannelId("com.myapp.first_notification");
builder.setSmallIcon(R.drawable.logo);
When creating a notification in android Oreo everything works until I use .setStyle then it stops working
Code:
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int importance = NotificationManager.IMPORTANCE_HIGH;
{
NotificationChannel mChannel = new NotificationChannel(
CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
assert notificationManager != null;
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle(track.getTitle())
.setContentText(track.getArtist())
.setSmallIcon(R.drawable.icon)
.setOnlyAlertOnce(true)//show notification for only first time
.setLights(Color.CYAN, 500, 1200) //Setting the color of the led blink on the phone for notifications
.addAction(drw_previous, "Previous", pendingIntentPrevious) //Setting action for the previous button
.addAction(playbutton, "Play", pendingIntentPlay) //Setting the action for the play/pause button
.addAction(drw_next, "Next", pendingIntentNext) //Setting the action for he next button
.setDeleteIntent(pendingIntentDelete) //Setting delete intent for when the notification is destroyed/deleted
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2)
.setMediaSession(mediaSessionCompat.getSessionToken())) //Setting how and what order the buttons will be shown
.setShowWhen(false) //Do not show time for when this notification was created
.setDefaults(Notification.DEFAULT_ALL); //Defaults
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
notificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}
Am I missing something. I have everything that should make it work. It used to work, but I am revisiting code changing it and making it better and not sure what went wrong this time. Your help is appreciated!
I'm programming some Android App that must make some Notification/Alarm after event occurs. Im wondering is there any function for NotificationManager like requireInteraction()?
Now when the certain event occurs the app just shows one notification for 1 sec, that's it..i'd like user to click OK to stop this vibration/sound
I found some code for notification from here:
NotificationCompat.Builder deprecated in Android O
Thanks #Mehul
public void showNotification (String from, String notification,
Intent intent) {
int requestID = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
requestID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
Notification mNotification = builder
.setContentTitle(from)
.setContentText(notification)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.build();
notificationManager.notify(/*notification id*/requestID, mNotification);
}
This one shows notification and it doesnt wait for user Input
If there is a need to add the buttons or action after clicking on notification you can use in your builder:
To add an button:
.addAction(new NotificationCompat.Action(*icon*, "Title", *intent for notification*));
or to add action that happen after user click the notification
.setContentIntent(*intent*);
Check the documentation about tab action and actions if you need more details.
I'am using FCM for getting new notification which I am able to show without any problem but when i open the app all my notification disappeared
this my code for creating new notification
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext,mContext.getString(R.string.default_notification_channel_id));
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setStyle(inboxStyle)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(mContext.getString(R.string.default_notification_channel_id), name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
int id = (int) System.currentTimeMillis();
notificationManager.notify(id, notification);
when click on a notification it opens an activity with extra data in
my intent
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
resultIntent.putExtra("message", "wwwwwwww");
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
now when i click on any notification it opens MainActivity with some extra value on which Main activity depends.
But when i click on a notification and it launches the activity all other notification disappeared.So, how can i prevent that and hide only clicked notification so user can see other notification
not quite sure whether it'll work or not but can u please try with setting the request code unique rather than 0.
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
replace 0 with the following
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,(int) System.currentTimeMillis(),intent,PendingIntent.FLAG_UPDATE_CURRENT);
try this
**.setPriority(NotificationManager.IMPORTANCE_HIGH)**
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(ICON)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(num, notification);