I used firebase cloud functions to the device to device notifications but notifications not working on versions below oreo.
private void sendNotification1(String notificationTitle, String notificationBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 0,new Intent(this, MyReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
RemoteInput remoteInput = new RemoteInput.Builder(NOTIFICATION_REPLY)
.setLabel("Respond to Message")
.build();
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(android.R.drawable.ic_delete,
"Reply Now...", pendingIntent1)
.addRemoteInput(remoteInput)
.build();
Here I am creating notification channel for oreo
NotificationChannel notificationChannel = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH );
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ONE_ID)
.setAutoCancel(true) //Automatically delete the notification
.setSmallIcon(R.mipmap.ic_launcher)//Notification icon
.addAction(action)
.addAction(R.drawable.accept, "ACCEPT", pendingIntent)
.addAction(R.drawable.delete, "DECLINE", pendingIntent)
.setContentIntent(pendingIntent)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setSound(defaultSoundUri);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(1, notificationBuilder.build());
}
I also mentioned the entry in Manifest too. And receiving notifications on oreo properly but below oreo version notifications are invisible. please help me out.
In Oreo Notification is showing using chanel id as in your code. But below oreo version chanel id is not required to use.
Check if your getting log while you receive notification from friebase. If you are getting log then notification functionality is working properly, you are not correctly notifying the device.
Related
I can see on MI phone android version 10. i am trying to create a simple notification with title and subtitle. notification also shows up but with no sound and no vibration.
PFB the code:
Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.app_icon_white);
notificationBuilder.setColor(getResources().getColor(R.color.theme_color));
} else {
notificationBuilder.setSmallIcon(R.drawable.app_icon_white);
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH);
CharSequence name = getResources().getString(R.string.feroz_channel_name);
String description = getResources().getString(R.string.feroz_channel_description);
channel.enableLights(true);
channel.setLightColor(getResources().getColor(R.color.theme_color));
channel.enableVibration(true);
channel.setDescription(description);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(77 /* ID of notification */, notificationBuilder.build());
I have vibration permission in Android Manifest. Along with no sound and no vibration. this notification is not visible when device is in locked state. Please suggest what i have missed in the code to achieve this scenario on android 10.
I am working on Android App with Firebase notification service FCM. and I recives notification successfully. but after I recive it is not showin in Android 10 (it work in 9 and 8 and smaller versions).
This is my showing notification code, I dont know what I missed. I check about if there is any changed with 10 but I dont see that...
My method:
private void sendNotification(String messageBody, String title) {
Intent intent;
if(type.equals("order_pending") || type.equals("order_declined")){
intent = new Intent(this, myOrdersActivity.class);
}else { 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 = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.logo)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int f =0;
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
// EventBus.getDefault().post(eventObject);
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("isActive", true)) {
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
Intent dialogIntent = new Intent(this, MainActivity.class)
.putExtra("ac", "1");
// sendBroadcast(dialogIntent);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
f =1;
// startActivity(dialogIntent);
}
if(f==0) {
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
Any help please?
and Thanks
The ship has sailed, but:
worked skeleton (Java):
void sendNotif(int icon, String sText) {
String channel_id = "1002";
String sTitle = getString(R.string.app_name);
Intent notificationIntent = new Intent(this, activityMain.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Android 8.0 (API level 26) and higher
// https://developer.android.com/training/notify-user/build-notification
CharSequence name = "statusbar";
String description = getString(R.string.app_name);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(channel_id, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channel_id)
.setSmallIcon(icon)
/*.setContentTitle(sTitle)*/
.setContentText(sText)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(contentIntent)
.setAutoCancel(false);
// notificationId is a unique int for each notification that you must define
// and remember for delete notification
notificationManager.notify(0, builder.build());
}
else {
// Android 7.1 (API level 25) and lower
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(getApplicationContext())
.setTicker(sTitle)
.setContentTitle(sTitle)
.setContentText(sText)
.setSmallIcon(icon)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
// for BIG icon:
//notification.contentView.setImageViewResource(android.R.id.icon, R.drawable.icon);
mNotificationManager.notify(0, notification);
}
}
I write this code to show a notification in android. But this notification is only shown in the status bar. I want when a message received show notifications on the top screen like telegram app:
enter image description here
My code:
NotificationManager mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String offerChannelId = "offerChannelId";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String offerChannelName = "offerChannelName";
String offerChannelDescription = "offerChannelDescription";
int offerChannelImportance = NotificationManager.IMPORTANCE_HIGH;
#SuppressLint("WrongConstant") NotificationChannel notifChannel = new NotificationChannel(offerChannelId, offerChannelName, offerChannelImportance);
notifChannel.setDescription(offerChannelDescription);
mNotifyManager.createNotificationChannel(notifChannel);
}
NotificationCompat.Builder sNotifBuilder = new NotificationCompat.Builder(getBaseContext(), offerChannelId);
sNotifBuilder.setSmallIcon(R.drawable.ic_notifications)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_notifications))
.setColor(getResources().getColor(R.color.baseColor_yellow))
.setContentTitle("title")
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_MAX);
mNotifyManager.notify(1, sNotifBuilder.build());
How I do it?
Min SDK is 21.
Thanks in advance.
I found a solution for you. Hope it may help you.
//build notification
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Ping Notification")
.setContentText("Tomorrow will be your birthday.")
.setDefaults(Notification.DEFAULT_ALL) // must requires VIBRATE permission
.setPriority(NotificationCompat.PRIORITY_HIGH) //must give priority to High, Max which will considered as heads-up notification
.addAction(R.drawable.dismiss, getString(R.string.dismiss), piDismiss)
.addAction(R.drawable.snooze, getString(R.string.snooze), piSnooze);
//set intents and pending intents to call service on click of "dismiss" action button of notification
Intent dismissIntent = new Intent(this, MyService.class);
dismissIntent.setAction(ACTION_DISMISS);
PendingIntent piDismiss = PendingIntent.getService(this, 0, dismissIntent, 0);
//set intents and pending intents to call service on click of "snooze" action button of notification
Intent snoozeIntent = new Intent(this, MyService.class);
snoozeIntent.setAction(ACTION_SNOOZE);
PendingIntent piSnooze = PendingIntent.getService(this, 0, snoozeIntent, 0);
// Gets an instance of the NotificationManager service
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/* Notification for oreo*/
String channelId = "channel-01";
String channelName = "Demo";
int importance = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
notificationManager.createNotificationChannel(mChannel);
}
//to post your notification to the notification bar with a id. If a notification with same id already exists, it will get replaced with updated information.
notificationManager.notify(0, builder.build());
Minimum SDK is Lolipop.
I am working on an application which sends device to device push notification. I have created a custom notification layout which has a heading, message and two buttons (Accept and Reject). When device B receives a notification from Device A, for some phones it works perfectly, but in some phones the notification tray fails to display the title, message and buttons. It just shows a blank notification tray. Its not an error, but the custom layout doesn't load for some phones. It works perfectly in Moto G5s plus (Android 7.1.1), but doesn't work for RedMi Note 5 Pro, same API level (Android 7.1.1). Can anyone help me with this?
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> remoteMessageData = remoteMessage.getData();
String remoteMessageType = remoteMessageData.get("type");
String message = remoteMessageData.get("message") + ". Please Confirm?";
String profilePhoto = remoteMessageData.get("profile_photo");
String notificationUID = remoteMessageData.get("notification_uid");
String userUID = remoteMessageData.get("user_uid");
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_custom_notification);
remoteViews.setTextViewText(R.id.textNotificationMessage, message);
remoteViews.setImageViewBitmap(R.id.eIntercomProfilePic, getBitmapFromURL(profilePhoto));
Notification notification = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
.setSmallIcon(R.drawable.namma_apartment_notification)
.setAutoCancel(true)
.setCustomBigContentView(remoteViews)
.setSound(RingtoneManager.getDefaultUri(Notification.DEFAULT_SOUND))
.setPriority(PRIORITY_DEFAULT)
.build();
int mNotificationID = (int) System.currentTimeMillis();
Intent acceptButtonIntent = new Intent("accept_button_clicked");
acceptButtonIntent.putExtra("Notification_Id", mNotificationID);
acceptButtonIntent.putExtra("Notification_UID", notificationUID);
acceptButtonIntent.putExtra("User_UID", userUID);
PendingIntent acceptPendingIntent = PendingIntent.getBroadcast(this, 123, acceptButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.buttonAccept, acceptPendingIntent);
Intent rejectButtonIntent = new Intent("reject_button_clicked");
rejectButtonIntent.putExtra("Notification_UID", notificationUID);
rejectButtonIntent.putExtra("Notification_Id", mNotificationID);
rejectButtonIntent.putExtra("User_UID", userUID);
PendingIntent rejectPendingIntent = PendingIntent.getBroadcast(this, 123, rejectButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.buttonReject, rejectPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
/*To support Android Oreo Devices and higher*/
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
getString(R.string.default_notification_channel_id), "Namma Apartments Channel", NotificationManager.IMPORTANCE_HIGH);
Objects.requireNonNull(notificationManager).createNotificationChannel(mChannel);
}
}
i think you talking about Oreo or above version. Oreo can not support notification for notification you need to create channel for that like below :
private void sendMyNotification(String message,String title) {
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
#SuppressLint("WrongConstant")
NotificationChannel notificationChannel=new NotificationChannel("my_notification","n_channel",NotificationManager.IMPORTANCE_MAX);
notificationChannel.setDescription("description");
notificationChannel.setName("Channel Name");
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.listlogo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.tlogo))
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationManager.IMPORTANCE_MAX)
.setOnlyAlertOnce(true)
.setChannelId("my_notification")
.setColor(Color.parseColor("#3F5996"));
//.setProgress(100,50,false);
notificationManager.notify(0, notificationBuilder.build());
}
Below mentioned code is working for all devices below android O verison. For android O,
addAction() method is not working i.e. button click is not working in android O.
Any help would be appreciated.
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mediaPlayerReceiver = new Intent("com.consult.news.receiver.ACTION_PLAY");
mediaPlayerReceiver.putExtra("NewsArticle", news);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, mediaPlayerReceiver, PendingIntent.FLAG_UPDATE_CURRENT);
Intent dismissNotification = new Intent("com.consult.news.receiver.DISMISS");
dismissNotification.putExtra("Notification_ID", 1);
PendingIntent dismissNotificationIntent = PendingIntent.getBroadcast(context, 0, dismissNotification, PendingIntent.FLAG_UPDATE_CURRENT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String CHANNEL_ID = "my_channel_01";
String CHANNEL_NAME = "my Channel Name";
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "my_channel_01")
.setSmallIcon(R.drawable.ic_notification_white)
.setColor(ContextCompat.getColor(context, R.color.accent))
.setContentTitle(context.getString(R.string.Consult_Univadis_Title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(news))
.addAction(isPlaying ? R.drawable.ic_notification_white : R.drawable.ic_notification_white, isPlaying ? "Play" : "Pause", pendingIntent)
.addAction(R.drawable.ic_notification_white, "Close", dismissNotificationIntent)
.setOngoing(true)
.setAutoCancel(false);
notificationManager.notify(1, builder.build());
I ran into the same, in Android Oreo you need to make it a explicit Intent (is not enough with putting the receiver on the manifest, in fact, it won't pay attention to that), so when you make the intent, make it explicit using the setClass method:
Intent mediaPlayerReceiver = new Intent("com.consult.news.receiver.ACTION_PLAY");
mediaPlayerReceiver.putExtra("NewsArticle", news);
mediaPlayerReceiver.setClass(this, YourReceiver.class);
Where "this" is the Context and YourReceiver, is the Receiver class that you are expecting to listen to the action.
You will have to do the same for the dismissNotification intent
Let me know if this worked for you.