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());
}
Related
We are trying to implement heads up notification for our app. Before asking question here, we went through all the threads that discussed this issue. Taking references, we went ahead implementing the code, but still heads up notification does generate the push but not in the heads up fashion (like whatsapp), despite creating channel as needed in version above oreo and setting priority and importance to max and high respectively.
The particular code is as given below
private void sendMyNotification(String title, String message, String click_action, String uri, String tag, String nid) {
//On click of notification it redirect to this Activity
Intent intent = new Intent(click_action);
intent.putExtra("uri", uri);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
int notification_id = nid!=null ? Integer.parseInt(nid) : MainActivity.ASWV_FCM_ID;
String channelId = MainActivity.asw_fcm_channel;
Uri soundUri= Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + BuildConfig.APPLICATION_ID + "/" + R.raw.adhan);
NotificationChannel channel = new NotificationChannel(channelId, "name",
NotificationManager.IMPORTANCE_HIGH); // for heads-up notifications
channel.setDescription("description");
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, MainActivity.asw_fcm_channel)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
Notification noti = notificationBuilder.build();
noti.flags = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
notificationManager.notify(notification_id, notificationBuilder.build());
}
}
Shall appreciate should you guide us as where we are going wrong.
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.
I am building an app which shows notification when a new message arrives.I have successfully implemented the notification part-but there, I am facing some weird issue while viewing my shown notifications -it is showing different content text based on different scenarios-I am including the 2 cases-
a)when the notifications arrives
b)when I slide down the notification tray to see my notifications
As you can clearly see the differences in the content-text in two different cases- Why is it happening ?
I am attaching the code which I have implemented to build the notifications-
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String identifyDataType = remoteMessage.getData().get(getString(R.string.data_type));
//SITUATION: Application is in foreground then only send priority notificaitons such as an admin notification
Log.d(TAG, "Data type: data: " +identifyDataType);
if(identifyDataType.equals(getString(R.string.data_type_admin_broadcast))){
//build admin broadcast notification
String title = remoteMessage.getData().get(getString(R.string.data_title));
String senderId=remoteMessage.getData().get(getString(R.string.sender_of_message));
String message = remoteMessage.getData().get(getString(R.string.data_message));
Log.d(TAG,"TITL AND MESSG"+title+message);
sendBroadcastNotification(title, message,senderId);
}
}
#RequiresApi(api = Build.VERSION_CODES.O)
private void sendBroadcastNotification(String title, String message, String senderId){
Log.d(TAG, "sendBroadcastNotification: building a admin broadcast notification with "+title+" "+message);
NotificationChannel mChannel=null;
// Instantiate a Builder object.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default_channel_id");
// Creates an Intent for the Activity
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.putExtra("Id", "none");
Log.d("messsagingService Title",title);
notifyIntent.putExtra("Name", title.split(":")[1].trim());
// Sets the Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Creates the PendingIntent
PendingIntent notifyPendingIntent =
PendingIntent.getActivity(
this,
0,
notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mChannel = new NotificationChannel("default_channel_id", getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
mChannel.setDescription(message);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
//add properties to the builder
builder.setSmallIcon(R.drawable.icons1)
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(),
R.drawable.icons1))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentTitle(title)
.setContentText(message)
.setColor(getResources().getColor(R.color.blue_btn_bg_color))
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(title).setSummaryText(message))
.setNumber(mNumPendingMessages)
.setOnlyAlertOnce(true);
builder.setContentIntent(notifyPendingIntent);
mNotificationManager.notify(BROADCAST_NOTIFICATION_ID, builder.build());
}
Any help is very much solicited -why this is even happening! Thank you.
I am working on an app where i am using custom Push Notification whenever there is an update from server that we need to send the notification, i am sending the notification using Notification builder and everything is working as expected, and i am redirecting the user to the relevant post on the ViewPager by mapping the Heading of the Notification.
I am attaching the code i am using
Notification Builder code
Intent resultIntentArticle = new Intent(context, HomeActivity.class);
resultIntentArticle.putExtra("pushNotificationClick", "yes");
resultIntentArticle.putExtra("heading", allArticles.get(0).getArticleHeading().trim());
resultIntentArticle.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
0, resultIntentArticle,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder = new NotificationCompat.Builder(context);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.mipmap.icon_not);
} else {
mBuilder.setSmallIcon(R.mipmap.icon_not);
}
mBuilder.setColor(getResources().getColor(R.color.colorPrimary));
mBuilder.setContentTitle("" + allArticles.get(0).getArticleHeading().trim())
.setContentText("" + allArticles.get(0).getArticleContent().trim())
.setAutoCancel(true)
.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify((int) System.currentTimeMillis() /* Request Code */, mBuilder.build());
currentNotificationStatus = "sent";
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("currentNotificationStatus" +
"", "" + currentNotificationStatus).apply();
I am sending two variables with PendingIntent i.e. pushNotificationClick and heading and am recieving it inside my Activity like this.
notificationClickedStatus = getIntent().getStringExtra("pushNotificationClick");
notificationHeading = getIntent().getStringExtra("heading");
Now the problem is that whenever we get 2-3 notifications and the user clicked on the very first notification that i have got, i am still getting the latest Notification heading in my activity. Earlier it was working completely fine, but now i am not getting any clue.
I'm struggling to get my FCM notifications to stack in the same way as for Whatsapp:
1) The latest message for a particular room is displayed, with an updated count of all unread messages.
2) Indicating separate groupings of point 1 for notifications for different rooms.
I have spent a few hours looking at various SO questions and this is the closest I've come to finding an answer. The issue is that I am using a data payload, not notification, and it doesn't seem like the "tag" property works for my data payload.
The current behavior is that only the latest notification shows, overriding the previous.
Here is the sendNotification() method in my FirebaseMessagingService
private void sendNotification(RemoteMessage remoteMessage) {
//Intent intent = new Intent(this, StudentChatActivity.class);
String clickAction = remoteMessage.getData().get("click_action");
Intent intent = new Intent(clickAction);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
String roomID = remoteMessage.getData().get("ROOMID");
String origin = remoteMessage.getData().get("ORIGIN");
String msgID = remoteMessage.getData().get("MSGID");
Log.d(TAG, "Message data payload, roomID: " + roomID);
intent.putExtra("ROOMID", roomID);
intent.putExtra("USERID", UserID);
intent.putExtra("USERNAME", UserName);
intent.putExtra("ORIGIN", origin);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.received_message);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.small_pickle)
.setContentTitle("FCM Message")
.setContentText(remoteMessage.getData().get("body"))
.setPriority(1)
.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_HIGH);
notificationManager.createNotificationChannel(channel);
}
Log.d(TAG, "Noification ID: " + notify_no);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}