This question already has answers here:
Android Push Notifications: Icon not displaying in notification, white square shown instead
(21 answers)
Closed 5 years ago.
I'm trying to build my own notification as soon as I receive a payload from my server in my FCM Receiver, I set my notification icon to my app icon which is in PNG Format, however, my notification is displayed with a white square instead and I don't know why..
Here's my code:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
.setContentTitle("Your chat is going to expire tomorrow!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, main_activity.class);
resultIntent.putExtra("launchedFromNotification",true);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your app to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(main_activity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mNotificationId is a unique integer your app uses to identify the
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
This usually happens when you use a non-alpha icon
When you use icons on your notification, android paints over all non-alpha (non-transparent) parts of the icon. This means if you use a square image as your notification icon, it would just come out as a white square.
To fix this, just use a shape-like icon. Check below for reference:
Icon Reference
Related
I am trying to create a notification on android in order to notify the user if something is happening. Here is my example code (called from the MainActivity)
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.stoxx_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 1001;
mNotificationManager.notify(mId, mBuilder.build());
The notification is shown on the screen (no lock screen, the normal un-locked screen) and when I swipe the notification bar down (like Figure 2 in the documentation) I can see it as well, but the text is like follows:
MyApplication
Contents hidden
I was expecting to see the following content:
My notification
Hello World!
I looked in the documentation but did not find anything that I can use to solve my problem(I looked at setVisibility, but this seems to be relevant for a lock-screen only which I do not have here). Maybe I overlooked it, but maybe this does nothing have to do with a Notification. Any idea how to solve this problem?
Setting setVisibility(NotificationCompat.VISIBILITY_PUBLIC) makes the text readable even on the lock screen (not what I want) and setVisibility(NotificationCompat.VISIBILITY_SECRET) does not show the notification on the lock screen at all.
What I want:
lock-screen: notification is shown with 'Contents hidden'
unlocked screen: notification is shown with content as given in the code.
What I get with VISIBILITY_PRIVATE:
lock-screen: notification is shown with 'Contents hidden'
unlocked screen: notification is shown with 'Contents hidden'
What I get with VISIBILITY_PUBLIC:
lock-screen: notification is shown with with content as given in the code.
unlocked screen: notification is shown with with content as given in the code.
What I get with VISIBILITY_SECRET:
lock-screen: notification is not shown at all.
unlocked screen: unknown
I'd like an activity of my app to listen when a specific notification is received and do something then.
The notification is triggered from within the app itself. I'm kind of new to Android development (and to development in general) so here's some pseudocode for what I want to do:
Activity{
notificationListener(){
if(notification is received){
//Do something
}
}
Any help would be much appreciated.
After checking the API of Notification, there seems no way to listen to sending Notification. After thinking deep, you would understand why we can't. AFAYK, it is we that write codes to send Notification but not the system, right? We surely know when we send Notifications, right? So, if you want to know when Notification is sent, why not do something, like sending BroadcastReceiver or starting Service to let other codes noticed? It all depends on our code-writers. And here is how to customize a Notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
I am creating one notification application.
But application require text instead of icon/image.
Reference image:http://tinypic.com/view.php?pic=23hu5xd&s=6
I am using this way to create notification all of the work fine but require text on status bar.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("hiii") // title for notification
.setContentText("Hello word") // message for notification
.setAutoCancel(false); // clear notification after click
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
mNotificationManager.notify(0, mBuilder.build());
Problem:
how to set text instead of image. setSmallIcon(R.drawable.ic_launcher)
You have no choice but to set an image. You are welcome to have the image contain text.
In the screenshot you cited, the app probably has ~100 images to choose from for different percentage levels. You can organize those into a single LevelListDrawable, then use the two-parameter version of setSmallIcon() to indicate which level you want.
I have a method which receives text from a push notification, via the Parse API, and packages it into a notification object. Pretty standard stuff. My problem is that I'm trying to use a BigTextStyle to display my notification in the list, but it refuses to do so, and only shows one line of text and the two-finger gesture does not cause it to expand.
However, if I tap the notification, which opens the app, then return to the notification list, it is displayed in the BigTextStyle and is responsive to gestures. So, my guess is that somehow tapping on the notification is activating it and allowing the BigTextStyle code to kick in.
I like that tapping on the notification opens the app, but I don't want to force my users to open the app then close it again to see the full text of my messages. So is there a way I could either make the notification display in the BigTextStyle format from the start, or to make it so that the first click "activates" the notification, allowing the full message text to be seen, and then a second click opens the app? Any help would be appreciated.
Here is my code from the Notification method:
public void receiveNotification() {
NotificationCompat.BigTextStyle bts = new NotificationCompat.BigTextStyle();
bts.bigText(SplashActivity.globalDataString);
bts.setSummaryText("Tap to open app, swipe to dismiss message");
NotificationCompat.Builder m = new NotificationCompat.Builder(this);
m.setContentTitle("New Push Notification")
.setContentText(SplashActivity.globalDataString)
.setSmallIcon(R.drawable.app_icon)
.setStyle(bts)
.build();
Intent openApp = new Intent(this, MenuActivity.class);
// This ensures that navigating backward from the Activity leads out of
// the application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MenuActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(openApp);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
m.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(pushMessageID, m.build());
pushMessageID++;
//reset notification
flag1 = false;
}
EDIT: I think my problem is with where I'm calling my receiveNotification() method from. Right now I have it in the onCreate() method of my app's starting activity, which doesn't make much sense looking back. Should I put it in my broadcastReceiver class, or would there be a better place to put it?
Yes, the creation and display of the notification is usually done either in the broadcast receiver, or in an intent service started by the broadcast receiver. Only when the user taps the notification, the relevant activity is launched. You can see Google's client code sample here.
Explanation:
I have used Daemon Thread.
In my application when user send any file (.jpg,.txt,.pdf etc) then it will generate the individual notifications for every file.
Suppose there are 3 notifications.
When user Tap on any of Notifications then it will call the android.content.Intent.ACTION_VIEW according to the file and suggest openwith option.
Problem:
When user select notification it will be opened in respective application(e.g If user selects sample.txt then it will be opened in colornote)and
after that when user tap on another notification then it will do nothing and onwards same thing happens.
I have passed also unique ID's for Notifications..!
long timestamp=System.currentTimeMillis();
int i=(int) timestamp;
mNotificationManager.notify(i, mBuilder.build());
Please help me find the error.
You need to add PendingIntent to your notification builder:
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MyActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
Read this article: Creating a Simple Notification