Notification Builder not receiving full context - java

I am using below code for notification builder for receiving notification on android device from through firebase cloud function. I am receiving notification successfully. but The context of the notification is not completely visible as you can see in the image.
On the top left icon is displayed as grey and not showing the actual icon and the text is not shown completely.
How should I rectify the above issues?
Notification Builder
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
.setSmallIcon(R.drawable.finalicon)
.setLargeIcon(largeIcon)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(notificationSoundUri)
.setContentIntent(pendingIntent);

I would kindly suggest you to read NotificationCompat.BigTextStyle.
Helper class for generating large-format notifications that include a
lot of text. If the platform does not provide large-format
notifications, this method has no effect. The user will always see the
normal notification view.
.setStyle(new NotificationCompat.BigTextStyle().bigText("YourText"))
Apply NotificationCompat.BigTextStyle to display text in the expanded content area of the notification. Kindly visit official guide line about Add a large block of text.
FYI
Android uses these values whenever incoming messages do not explicitly set icon or color.
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/your_image" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorAccent" />
Read official guideline about Set up a Firebase Cloud Messaging client.

Related

set custom Sound from only my application in push notification android

I need to set custom sound for only my application. I've assigned a custom ringtone from my storage to be used in notifications.
What did I try...
(NotificationChannel) channel.setSound(Uri.parse("MY_PATH"), audio_atrr);
(NotificationCompact.Builder) mBuilder.setSound(Uri.parse("MY_PATH"));
(Notification) mNotification.sound = Uri.parse("MY_PATH");
It's useless for me, my device always plays the default sound when a notification is coming. what can i do?

Show notifications on lock screen of Xiaomi devices

Push notifications not shows on lock screen of Xiaomi devices.
I've tried to use VISIBILITY_PUBLIC in notification builder and channel but it doesn't work.
The problem is that Xiaomi devices has special permission in apps notifications settings which permits to show notification at lock screen. But this permission is turn off by default. But in some apps like "Telegram" this permission is on by default after installation from google play, I can't find the solution how to do that.
Not sure if this helps, but I had a similar problem an a Huawei Device (API 29).
I wanted to use NotificationManager.IMPORTANCE_LOW on my NotificationChannel, but when I was trying to send Notifications on this Huawei Device, they where not Visible on the Lock Screen.
I figured out that there is an App Notification Option on this Huawei Device to use "gentle notifications". Those Notifications are not shown on the Lock Screen and this Option is turned on by Default if your Channel uses IMPORTANCE_LOW or below.
Changing the Importance of the Channel to IMPORTANCE_DEFAULT was solving my Problem.
Since I wanted IMPORTANCE_LOW because I dont wanted an Notification Sound, I just had to do a little workaround and set setSound(null, null) and setVibrationPattern(null).
NotificationChannel nChannel1 = new NotificationChannel(CHANNEL_1_ID, "Channel Name", NotificationManager.IMPORTANCE_DEFAULT);
nChannel1.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
nChannel1.setSound(null, null);
nChannel1.setVibrationPattern(null);
nChannel1.setDescription("Description");
nManager = context.getSystemService(NotificationManager.class);
nManager.createNotificationChannel(nChannel1);
Notification notification = new NotificationCompat.Builder(applicationContext, CHANNEL_1_ID)
.setContentTitle("title")
.setContentText("text")
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.build();
nManager.notify(1, notification);

notification displayed differently depending on whether app is in foreground or background

I'm having trouble with notifications on Android. I use the code below to generate a notification on the device whenever a GCM message is received by my app. However, it's producing unexpected results.
public class MyGcmListenerService extends GcmListenerService implements Constants {
private static final String TAG = "MyGcmListenerService";
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("msg");
sendNotification(message);
}
private void sendNotification(String message) {
NotificationManager notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.gcm_notification_channel_name);
String description = getString(R.string.gcm_notification_channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(GCM_NOTIFICATION_CHANNEL_ID, name,
importance);
channel.setDescription(description);
channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
channel.enableVibration(true);
channel.setSound(defaultSoundUri,
new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
.build());
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,
GCM_NOTIFICATION_CHANNEL_ID)
.setContentText(message)
.setContentTitle("My Title")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_notification);
notificationManager.notify(0, notificationBuilder.build());
}
}
If a GCM message is received whilst the app is open (not just running, but actually open on the screen), then the resulting notification has only a title, and doesn't show the message. This screenshot demonstrates this case. Further, if I send multiple GCM messages to the device whilst the app is in the foreground, only one is displayed.
If a message is received whilst the app is either closed or running in the background, the resulting notification shows only the message, and has no title. This screenshot shows the two messages side-by-side - the bottom one was received with the app in the foreground, the top was received with the app in the background. If multiple messages are received whilst the app is in the background, all are displayed (in contrast to what happens when the app is in the foreground). This screenshot shows that multiple messages are displayed when they're received with the app in the background.
Also, the notification only appears as heads-up when the app is in the foreground.
Finally, if received in the foreground, the resulting notification does nothing when tapped. However, if received in the background, the notification opens the app when tapped. Not really bothered by this, just thought it might be indicative of the problem.
FYI: when testing, I tried both keeping the GCM message the same every time, as well as varying it. Both scenarios gave the same result.
What I'd like to figure out:
How to get both the title and message to display regardless of whether app is in foreground or not. This is the most important.
How to get the notification to appear as heads-up when the app is in the background.
Just to pre-empt any responses saying not to abuse heads-up, it's the most important feature of the app (the app must notify users of certain events in real-time), according to our users.
Update:
Bas van Stein's answer allowed me to figure out why the either only the title or message was displayed.
As he correctly pointed out, when the app is in the background, GCM messages are handled by the system. This drove me to inspect the script that is used to send messages by the backend. I realised that the person who wrote this script had sent the message within the title field of the notification field of the GCM message, and there was no body field. So I corrected this issue, and the notifications displayed correctly (for app in background).
This also allowed me to realise that the line String message = data.getString("msg"); in onMessageReceived was returning null. I changed the method as follows:
public void onMessageReceived(String from, Bundle data) {
Bundle notification = data.getBundle("notification");
String title = notification.getString("title");
String message = notification.getString("body");
sendNotification(title, message);
}
Then I added title as a parameter to sendNotification and changed the line that sets the notification title to: .setContentTitle(title). Now notifications are displayed correctly when the app is in the foreground.
Further, I added a static int to the class that I use as the notification ID (incremented every time), so now multiple notifications display correctly.
Still not solved:
I'm still unable to have notifications appear as heads-up when the app is in the background. I tried adding "priority": "high" to the GCM message notification payload, but this had no effect - apparently this is the default for a GCM notification anyway.
This answer will be only a part of the complete solution but here we go:
First issue, the background and foreground notifications seem to be generated by two different functions, you can test this by applying a break point in your code and attach the debugger. You will likely see that the background notification is not triggering the break point in this code. Perhaps you miss a manifest service?
Second issue, that only one notification is being shown is because of this line:
notificationManager.notify(0, notificationBuilder.build());
The 0 here is the notification id, if you create multiple notifications with the same id it will overwrite the notification instead of showing a new one.
Third issue, that the application is not opened on notification tab, is because there is no intent attached to the notification you generate in your code.
You can use something like this for an intent:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
notificationBuilder.setContentIntent(pIntent);
The intent is being called when you click on the notification, this could be any intent so you can open a special activity for example.
Hope this brings you in the correct direction.

Android Wear notification with auto focus like Gmap

I want to create an auto-focused notification.
I have noticed that notifications have two states in Android Wear :
FOCUSED, the time is invisible. When you start a navigation on Google Map, the default notification looks like this :
NOT FOCUSED, the time is visible :
You can switch from one state to another by gently dragging up or down the white notification square.
But on my app and unlike Gmap, the notification appears not focused.
My code on the wearable side (required for onGoing notifications) :
mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentText("text")
.setOngoing(true)
.extend(new NotificationCompat.WearableExtender()
.setBackground(logoBitmap)
.setHintHideIcon(true)
);
(getSystemService(NOTIFICATION_SERVICE)
.notify(NOTIFICATION_ID, mBuilder.build());
My notification appears unfocused like this :
I tried setFullScreenIntent() but without any result.
I finally understood. Using setVibrate() extends the notification automatically. This is not mentioned anywhere in the doc, I just get lucky on this.

Running Android App in background or as a constant notification

I'm learning how to make apps for android and I have started by creating one which makes my phone scream when its dropped.
I got it working to where the phone screams when dropped, but now I need to make it so that the phone screams when dropped even when the app is closed, and to show a notification in the notification bar saying that its running
What should I use to do this? Should I use intentService? Ive been looking all over and I'm not sure where to look. Any guides would be appreciated
You need to make your service run in the foreground. You can achieve this by showing a notification when your service is running.
This is how you need to make your service run as foreground
private void showNotification(String title)
{
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title);
startForeground(1000,mBuilder.build()); // 1000 - is Id for the notification
}
You can also set your custom RemoteViews in notification using setContent
You can remove the service from foreground state using stopForeground
check Service Training.
For your use case it's important that it's an foreground service:
Documentation Service

Categories

Resources