I'm playing around notifications in android, and I'm wondering why NotificationCompat doesn't display Large Icon, and Number in Gingerbread as it does in Jellybean (see pics), I thought that was for that purpose that it was created ?
here is how I fire the notifications :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnShow = (Button)findViewById(R.id.btnNotif);
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setWhen(System.currentTimeMillis())
.setContentText("You are near your point of interest.")
.setContentTitle("Proximity Alert!")
.setSmallIcon(android.R.drawable.ic_menu_info_details)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.orchide))
.setAutoCancel(true)
.setTicker("Proximity Alert!")
.setNumber(10)
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND);
/*Create notification with builder*/
notification=notificationBuilder.build();
/*sending notification to system.Here we use unique id (when)for making different each notification
* if we use same id,then first notification replace by the last notification*/
btnShow.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
notificationManager.notify(1000, notification);
}
});
}
Large icon is ignored on pre-honeycomb API levels.
NotificationCompat.Builder documentation says:
...On platform versions that don't offer expanded notifications, methods that depend on expanded notifications have no effect...
If you look at the NotificationCompat.Builder source you'll see that large icon is used for honeycomb and above.
Related
I did like to ask how I can go about the implementation of a customized notification even when the phone is locked.
e.g., i have this service that helps with push notification already.
public class NotifyService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate(){
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(), HomeMenuActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
Notification mNotify = new Notification.Builder(this)
.setContentTitle("Checker")
.setContentText("Welcome here")
.setSmallIcon(R.drawable.app_icon)
.setContentIntent(pIntent)
.setSound(sound)
.addAction(0, "Loading...", pIntent)
.build();
mNM.notify(1, mNotify);
}
}
But, I did like to know how I can go about the implementation of something like the attached image below.
the appearing of notification on lock screen is started from lollipop version.So you need to set priority max or high of notification then it will be shown on lock screen when device is locked and also will be shown as heads up notification.
I'm using Android Studio. I wanna add message with number of notifications. Why when I run app there is no content info on the screen?
>
private int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void notyfikacja(View view) {
Intent intent = new Intent(this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder
.setSmallIcon(android.R.drawable.arrow_up_float)
.setContentInfo("You have"+ ++i + "messages" )
.setContentText("ContentText")
.setContentTitle("ContentTitle")
.setAutoCancel(true)
.setContentIntent(pendingIntent);
Notification notification = builder.build();
NotificationManagerCompat.from(this).notify(0, notification);
}
I add the Notification Screeshot
I am not sure about why the setContentInfo doesn't work. Probably cause you are trying in Nougat ? Anyways, according to the documentation, it is recommended to use setSubText(CharSequence) instead of setContentInfo(CharSequence info)
From Doc:
This method was deprecated in API level 24. use setSubText(CharSequence) instead to set a text in the header. For legacy apps targeting a version below N this field will still show up, but the subtext will take precedence.
I am trying to create a notification for android device. This is the code written on the mobile. It shows the notification on the mobile phone but does not show up in android wear. Am I missing out on something?
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Log.i("Tag","Inside Self destruct");
//Code for notification begins here
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, ShowNotificationDetailActivity.class), 0);
Resources r = getResources();
String greetingIdText = r.getString(R.string.notification_text);
Notification notification = new NotificationCompat.Builder(context)
.setTicker(r.getString(R.string.notification_title))
.setSmallIcon(android.R.drawable.ic_menu_report_image)
.setContentTitle(r.getString(R.string.notification_title))
.setContentText(greetingOne.getContent())
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(0, notification);
}
});
am following goolge's guide on how to implement notifications on wearables
Am developing on an emulator, but the damned thing won't take notifications from either another emulator or even physical device .. I need some guidance please.
here's my code
Intent viewIntent = new Intent(getApplicationContext(),
Not.class);
// viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent = PendingIntent.getActivity(
getApplicationContext(), 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Done").setContentText("In my face")
.setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager = NotificationManagerCompat
.from(getApplicationContext());
// Build the notification and issues it with notification
// manager.
notificationManager.notify(1,
notificationBuilder.build());
Create mobile app whitout wear.
Then insert this code.
Then run app on handheld emulator. And see the notification on wear emulator. (May be needed to slide down to find it)
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Intent intent = new Intent(this, MyActivity2.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MyActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action action = new NotificationCompat.Action.Builder(
R.drawable.ic_launcher,
getString(R.string.wearTitle),
pendingIntent).build();
Notification notification = new NotificationCompat.Builder(MyActivity.this)
.setContentText("title")
.setContentText("content")
.setSmallIcon(R.drawable.ic_launcher)
.extend(new NotificationCompat.WearableExtender().addAction(action))
.build();
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(001, notification);
}
}
The code i'm putting in onCreate() is the following:
if(Build.VERSION.SDK_INT >= 11)
notifApiGT10();
else
notifApiLT10();
Where,
#SuppressWarnings("deprecation")
#SuppressLint("NewApi")
private void notifApiGT10() {
// TODO Auto-generated method stub
Notification.Builder builder ;
NotificationManager notifier;
builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setOngoing(true).setContentTitle("my Title").setContentText("my displayed text");
notifier = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT>=11 && Build.VERSION.SDK_INT<=15)
notifier.notify(1, builder.getNotification());
else if (Build.VERSION.SDK_INT > 15)
notifier.notify(1, builder.build());
}
private void notifApiLT10()
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentText("is actually in use")
.setContentTitle("my Title")
.setOngoing(true)
.setTicker("my ticker")
.setSmallIcon(R.drawable.ic_launcher);
Notification notif = builder.getNotification();
NotificationManager mN = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mN.notify(1, notif);
}
The above code is not working on API 10. My device is running Gingerbread and it's not working on it.
I wonder why...Any experts?
SOLUTION:
Notification notification = new Notification(R.drawable.ic_launcher, "my ticker", System.currentTimeMillis());
notification.flags = Notification.FLAG_ONGOING_EVENT;
Context context = getApplicationContext();
Intent i = new Intent(this,Main.class);
PendingIntent pd = PendingIntent.getActivity(this, 1, null, Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(context, "my title", "my text", pendingintent);
NotificationManager mN = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mN.notify(1,notification);
The above solution solved my problem,
even if you don't want to run anything on click, you should put the pendingintent inside the method setLatestEventInfo, but in pd as you noticed, in the 3rd field, i have set the intent to null
Have you tried using NotificationCompat.Builder.build()? NotificationCompat.Builder.getNotification() is deprecated.
FYI, last time I used NotificationCompat.Builder, setNumber() did not work properly and I ended up constructing Notification directly on older devices.
EDIT: Try using Notification class directly on older devices because NotificationCompatImplBase (API <= 10) uses only 4 fields from the builder no matter how many are set.