I have created custom notification, I am not able to show current time in my notification bar.. It is not coming even though I am showing System.currentTimeMillis().
final Notification notification = new Notification(R.drawable.notificationicon, context.getResources().getString(R.string.notificationheadingtext), System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.when = System.currentTimeMillis();
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion > 20)
{
notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_process);
}
else
{
notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_process_lowerversion);
}
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.sound = alarmSound;
notification.contentIntent = pendingIntent;
notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_launcher);
notification.contentView.setTextViewText(R.id.status_text, context.getResources().getString(R.string.notificationheadingtext));
notificationManager.notify(42, notification);
Notification class is deprecated. Use NotificationCompat. Read this tutorial
Related
I have implemented local notification with sound. It's working properly but when I clear local notification the custom sound what I am playing should stop.
Uri uri = null;
String[] split = content.split("-");
Intent intents = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intents, 0);
//getting audio from raw folder
uri = Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.beep);
//plays audio infinite times until user clicks or clear notification
try
{
MediaPlayer player = MediaPlayer.create(context, uri);
player.setLooping(true);
player.start();
Notification noti = new Notification.Builder(context)
.setContentTitle(split[0])
.setContentText(device_name + split[1]).setSmallIcon(R.drawable.hi_notification)
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.build();
noti.sound = uri;
noti.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
NotificationManager notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
notificationManager.notify(0, noti);
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
Set the notification sound to be INSISTENT.
1.Notification.DEFAULT_SOUND:used to play sound.
2.Notification.FLAG_INSISTENT:this flag is makes your sound to ring continuously until you have done few operations on notification ie, either you drag the bar or you click the bar.
3.Notification.FLAG_AUTO_CANCEL:this flag is used to automatically cancel the notification after you have seen it
builder.setSmallIcon(R.mipmap.app_icon).setContentTitle(title)
.setContentText(msg)
.setColor(getResources().getColor(R.color.white))
.addAction(R.drawable.alarm_off, getString(R.string.dismiss), pendingIntent)
.setOngoing(true)
.setSound(Uri.parse(path));
Notification notification = builder.build();
notification.flags |= Notification.FLAG_INSISTENT;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(WAKE_UP_ALARM_ID, notification)
I am getting errors with these lines while handling notifications for different API levels. This is how i did so far:
...
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Notification notification;
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB){
notification = new Notification(icon, text, time);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
}
else
{
notification = new Notification.Builder(this) // error
.setContentTitle(title) // in
.setContentText(tmp_task_brief) // these
.setSmallIcon(icon) // lines
.setLargeIcon(null) // telling "this method call requires API level 11
.build(); // or higher"
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
}
...
I don't understand how to remove these errors. Please help me.
Edit: I did applied edit as below but NotificationCompact.Builer too got deprecated method getNotification() that returns Notification object.
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB)
{
notification = new Notification(icon, text, time);
notification.setLatestEventInfo(this, title, text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
}
else
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(contentIntent)
.setSmallIcon(icon)
.setTicker(text)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(text);
notification = builder.getNotification();
mNM.notify(NOTIFICATION, notification);
}
Use NotificationCompact.Bulider from support liberary (V4 liberary) that supports from 1.6
i think that will solves your problem.
Finally, with the help of these guys i ended up to the solution of handling the deprecated methods:
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
notification = new Notification(icon, text, time);
notification.setLatestEventInfo(this, title, text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(text).setWhen(time)
.setAutoCancel(true).setContentTitle(title)
.setContentText(text).build();
mNM.notify(NOTIFICATION, notification);
}
i need to know if is it possible to cache layout remote view ?
if so how i can cache or clean the cache is there any example here i tried to google it i could't find an example how to cache layout remote view
for example how to cache this notification progress !
thank you for your time
updateTask = this;
//setup notification ID and information
Random r = new Random();
NOTIFICATION_ID = r.nextInt(80-65) + 1;
NOTIFICATION_ID++;
int icon = R.drawable.icon;
long when = System.currentTimeMillis();
notification = new Notification(icon, getString(R.string.app_name), when);
contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setTextViewText(R.id.text, "cool"+NOTIFICATION_ID);
notification.contentView = contentView;
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
Intent notificationIntent = new Intent();
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.setAction(Intent.ACTION_MAIN);
PendingIntent contentIntent = PendingIntent.getActivity(BabupMain.this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
mNotificationManager.notify(NOTIFICATION_ID, notification);
I reasearched this, and this code is what I got
The app crashes when I open the MainActivity
NotificationCompat.Builder builder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Get Other APW Co. Apps on Play!")
.setContentText("Want more? All our apps are free!");
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) getSystemmService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, builder.build());
Try this, Give context name getSystemmService(Context.NOTIFICATION_SERVICE);.
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_launcher,
"Notification!", System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "App Name";
String notificationText = Msg;
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog),
context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,
0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context, notificationTitle,
notificationText, pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
This will work:
notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_launcher,
"Notification!",
System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "Get Other APW Co. Apps on Play!";
String notificationText = ""Want more? All our apps are free!"";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(SOME_URL));
PendingIntent pendingIntent
= PendingIntent.getActivity(AndroidNotification.this,
0, myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context,
notificationTitle,
notificationText,
pendingIntent);
notificationManager.notify(1, myNotification);
}});
You have to specify contentIntent, i.e. the PendingIntent that will be executed when the item is clicked. It's mandatory, and you haven't specified it which causes the error.
You can do this in the builder or on the Notification:
in builder:
builder.setContentIntent(contentIntent);
Notification n = builder.build();
on Notification:
Notification n = builder.build();
n.contentIntent = contentIntent;
Only after can you send it to the NotificationManager:
mNotifyMgr.notify(mNotificationId, n);
The exact contentIntent value depends on what you want to do. See reference here:
http://developer.android.com/reference/android/app/Notification.html#contentIntent
See working example here: https://github.com/nheid/unitedcoders-android/blob/master/src/com/unitedcoders/android/examples/download/DownloadProgress.java
I am trying to play a sound, and flash the backlight of android phone using notification manager. I have used the following code. All the required permissions are there in the manifest file. But I am not sure why this is not giving any notification in emulator or in the device (htc wildfire). If you know any feasible solution please let me know
XYNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int NOTFICATION_ID = 1331;
Notification notifyDetails = new Notification();
notifyDetails.icon = R.drawable.icon12;
notifyDetails.tickerText = "Message Received!!!";
notifyDetails.when = System.currentTimeMillis();
notifyDetails.vibrate = new long[] {0,1000,1000,1000,1000,1000,1000,1000}; //vibrate;
Intent notifyIntent = new Intent(this, XYReceiverAppActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0,notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
CharSequence contentTitle = "XYs Notification";
CharSequence contentText = "Get back to XY HOME screen by clicking me";
notifyDetails.setLatestEventInfo(this, contentTitle, contentText, pIntent);
Uri xysound = Uri.parse("android.resource://" + getPackageName() +"/"+ "soundxy");
notifyDetails.ledARGB = Color.BLUE;
notifyDetails.ledOnMS = 10000;
notifyDetails.ledOffMS = 1000;
notifyDetails.flags |= Notification.FLAG_SHOW_LIGHTS;
notifyDetails.sound = xysound;
XYNotificationManager.notify(NOTFICATION_ID, notifyDetails);
The device is not vibrating neither is there any sound alert. LED light is same. how do I send the notification?
Here is a code I use in one of my programs, it always worked...
int icon = R.drawable.alerte;
CharSequence tickerText = getString(R.string.lieuproche);
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Intent intent = new Intent(getApplicationContext(),
ActivityToLaunch.class);
notification.setLatestEventInfo(
MainActivity.this,
"title",
"action", PendingIntent.getActivity(
this.getBaseContext(), 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT));
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(0, notification);