I need to show a notification when user do some actions on app.
I can get it working on versions 6, 7, 8 and 9, and some of these are samsung. But the Galaxy Note5 is the only device that doesn't work.
This is the code:
private void sendNotification(String messageBody) {
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 = "channel test 1";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_check_circle_black_24dp)
.setContentTitle("My App")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
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_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
I'd tried a lot of variations of that code according by another topics that I found.
The code doesn't show any error or warning.
Can it be a device issue?
Edit 1:
I got this code from Firabase quickstart app:
I finally solved the issue.
At Notifications settings, Samsung has an advanced setting that I had to enable too. The other devices has just one place to enable/disable app notification.
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 was following this tutorial and I'm using FCM to send push notifications, but for some reason the code seems to not work.
In onMessageRecieved() im building the notification, making the phone vibrate and show the notification, and for some reason the phone vibrates, but no notification is shown.
This is the code:
Intent intent=new Intent(this, WelcomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this
,0
,intent
,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(getString(R.string.app_name));
notificationBuilder.setContentText(message);
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(soundUri);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher));
notificationBuilder.setAutoCancel(true);
Vibrator v=(Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
If anyone could tell me what's wrong it'd be awesome.. Thanks :)
From your code and the fact that the notification doesn't appear, I assume you are testing on a device with Android version above 8.0.
For Android 8.0 (Oreo) and above, you need to use notification channels, in order to display the notification. Refer to this link for more details.
For Android 8 and above use Notification channel like below
Intent intent=new Intent(this, WelcomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this
,0
,intent
,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(getString(R.string.app_name));
notificationBuilder.setContentText(message);
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(soundUri);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher));
notificationBuilder.setAutoCancel(true);
Vibrator v=(Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
notificationBuilder.setContentIntent(pendingIntent);
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,
"Membership Alerts",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);*/
CharSequence name = getString(R.string.notification_channel_name);
String description = getString(R.string.notification_channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(channelId, name, importance);
channel.setDescription(description);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
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());
}
This issue has been solved. See my solution below.
I just completed converting my messaging app to FCM. You can see the process I have been through here. Now that it is done, my notifications no longer work. If my FirebaseMessagingService gets a message and the main app is not active, I create a notification on the phone I'm running on.
This has been running for years correctly on GCM. When I trace through the code, it all executes ok - just no notification shows up in the tray. I can't imagine what Firebase would have to do with this.
This is the code that gets called from the FirebaseMessagingService. This code has been running for years just fine . . .
public static void raiseNotification( String username, String mesText, int count)
{
String message = "From: " + username + " " + mesText;
if (count > 1)
{
count--;
message = message + " + " + count + " more";
}
NotificationCompat.Builder b = new NotificationCompat.Builder(GlobalStuff.GCT);
Intent intent = new Intent(GlobalStuff.GCT, MainActivity.class);
intent.putExtra("whattodo", username);
intent.setAction(Long.toString(System.currentTimeMillis())); //just to make it unique from the next one
PendingIntent pIntent = PendingIntent.getActivity(GlobalStuff.GCT, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
b.setContentTitle("New SafeTalk Message")
.setSmallIcon(R.drawable.ticon)
.setContentText(message)
.setTicker("New SafeTalk Message")
.setContentIntent(pIntent)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true);
//.addAction(R.drawable.smallredball, "Read Now", pIntent)
//.addAction(R.drawable.smallquestion, "hello there", pIntent);
NotificationManager mgr = (NotificationManager)GlobalStuff.GCT.getSystemService(NOTIFICATION_SERVICE);
mgr.notify(0, b.build());
}
This issue is solved. Once you write an app for Android, Google will have you working full time for the rest of your life just to keep it working. They are breaking change demons. Turns out this notification problem has nothing to do with Firebase (which is itself the mother of all breaking changes).
Google changed the requirements on how to send a notification in Oreo. Google designed this change so that if your app is running on Oreo and you haven't made the change your notification simply won't work - hope nobody was building notifications that were important. In Oreo they require a channelId.
Here is code that works in Oreo . . .
Actually this code does not completely work in Oreo. See my next post regarding notifications in Oreo
private void sendNotification(String messageBody) {
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 = 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.ic_stat_ic_notification)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
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_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
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.