I need to Rewrite all the deprecated methods and class.
private void showNotification(Peer peer, int id) {
CharSequence text = getString(id) + " " + peer.getName();
Notification notification = new Notification(R.drawable.notification, text, System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this, IPsecToolsActivity.class); //intent.setAction(ACTION_NOTIFICATION);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, getText(R.string.native_service_label), text, contentIntent); //setLatestEventInfo method has been deprecated
// Send the notification.
mNM.notify(peer.getName(), R.string.notify_peer_up, notification);
}
Note that the Notification is deprecated, too old.
Cannot use setLatestEventInfo method.
I need help rewrite it in alternative way.
The way I rewrite this is as following: Please let me know if I was right or not.
private void showNotification(Peer peer, int id) {
CharSequence text = getString(id) + " " + peer.getName();
Context context = this;
Notification notification = new Notification.Builder(context)
.setContentText(text)
.setSmallIcon(R.drawable.notification)
.setWhen(System.currentTimeMillis())
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this, IPsecToolsActivity.class);
//intent.setAction(ACTION_NOTIFICATION);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, 0);
// Send the notification.
mNM.notify(peer.getName(), R.string.notify_peer_up, notification);
}
Related
Months ago I wrote a code that my Notification worked as bellow :
NotificationManager NotiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String MyText = "NotificationText";
Notification mNotification = new Notification(R.mipmap.icon, MyText, System.currentTimeMillis() );
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotification.defaults |= Notification.DEFAULT_SOUND;
mNotification.defaults |= Notification.DEFAULT_VIBRATE;
String MyNotificationTitle = "AnyText";
String MyNotificationText = "HERE MORE TEXT";
Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("https://www.google.com"));
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent StartIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
mNotification.setLatestEventInfo(context.getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
NotiManager.notify(NOTIFY_ME_ID_LOGIN, mNotification);
But now, that I want to make an update it doesn't even let compile the APP because this line :
mNotification.setLatestEventInfo(context.getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
Is there any way to change the setLatestEventInfo or other way to create a Notification?
Is there any way to change the setLatestEventInfo
You are welcome to lower your compileSdkVersion, though that will introduce its own set of issues.
or other way to create a Notification?
As Blackbelt notes in a comment, NotificationCompat.Builder has been around for ~4 years and is the recommended way to create Notification objects today:
private void raiseNotification(String mimeType, File output,
Exception e) {
NotificationCompat.Builder b=new NotificationCompat.Builder(this);
b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);
if (e == null) {
b.setContentTitle(getString(R.string.download_complete))
.setContentText(getString(R.string.fun))
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setTicker(getString(R.string.download_complete));
Intent outbound=new Intent(Intent.ACTION_VIEW);
outbound.setDataAndType(Uri.fromFile(output), mimeType);
b.setContentIntent(PendingIntent.getActivity(this, 0, outbound, 0));
}
else {
b.setContentTitle(getString(R.string.exception))
.setContentText(e.getMessage())
.setSmallIcon(android.R.drawable.stat_notify_error)
.setTicker(getString(R.string.exception));
}
NotificationManager mgr=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mgr.notify(NOTIFY_ID, b.build());
}
(from this sample project, which is in this directory of sample projects all showing how to display Notifications)
I am creating a notification upon click I am asking it cancel a service. It just works fine below 4.4 (Kitkat). My app supports 2.2 (API 8 onwards)
On Kitkit (Nexus 5) The service isn't called at all. I am not where I am going wrong here? It just works fine even on version 4.3?
Here is what I have tried and working on every phone except Nexus 5(Kitkat)
createNotification("Click here to cancel notification!", "TestApp");
I am calling the above immediately after calling a particular service.
private void createNotification(String body,String title)
{
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
int unique_id = 007;
Intent nintent = new Intent(this,ServicetocancelAlarm.class);
PendingIntent pin = PendingIntent.getService(this,0, nintent, 0);
//set notify image
Notification n = new Notification(R.drawable.ic_launcher, body,java.lang.System.currentTimeMillis());
n.contentIntent = pin;
n.setLatestEventInfo(this, title, body, pin);
n.defaults = Notification.DEFAULT_ALL;
nm.notify(unique_id, n);
}
Can somebody help me out fixing this issue with KITKAT?
Update:
I tried the following:
Notification("Message", "This is Android Notification Message");
And Method
#SuppressWarnings("deprecation")
private void Notification(String notificationTitle, String notificationMessage)
{
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher, "A New Message",
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, TransparentActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(MainActivity.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify(10001, notification);
}
This piece of code works fine on debug mode but if Export the apk and install it. It just doesn't work at all. It doesn't get to the new activity. I am not sure what is wrong here.
I solved the problem by trying the following code:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 16)
{
Context context = RepeatService.this;
Intent notificationIntent = new Intent(context, ServicetocancelAlarm.class);
PendingIntent contentIntent = PendingIntent.getService(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = context.getResources();
Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker(res.getString(R.string.ticker))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.app_name))
.setContentText(res.getString(R.string.cancelText));
Notification n = builder.build();
nm.notify(007, n);
}
Else part has the usual notification code that just works fine below API 16..
try it out!!
I need to show a local notification in android in title bar when certain event occurs. I want the notification also play certain sound as well.
On tap of notification I want app should not be launched and notification should be cleared.
I am using this code but on tap of notification it opens my app again.
CharSequence title = "App title";
CharSequence message = "This to alert you that estimated time is getting complete.";
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Title", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, JobView.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(JobView.this, title, message, null);
notificationManager.notify(1010, notification);
CharSequence title = "App title";
CharSequence message = "This to alert you that estimated time is getting complete.";
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Title", System.currentTimeMillis());
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.YOUR_SOUND_FILE);
notificationManager.notify(1234, builder.build());
Just replace the File Reference from your Raw folder in the code and the code should work fine.
I hope this helps.
Try this:
CharSequence title = "App title";
CharSequence message = "This to alert you that estimated time is getting complete.";
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentTitle(title);
mBuilder.setSound(Uri.parse("android.resource://"
+ this.getPackageName() + "/" + R.raw.sound));
mBuilder.setContentText(message);
mBuilder.setTicker(message);
mBuilder.setWhen(System.currentTimeMillis());
NotificationManager notificationManager =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
mBuilder.setContentIntent(pendingIntent);
notificationManager.notify(1010, mBuilder.build());
Change R.raw.sound with path to your sound file
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 am using the following code to create a Notification inside the onRecieve method of my BroadcastReceiver but it is giving me the following exception:
java.lang.IllegalArgumentException: contentIntent required
The code:
NotificationManager notificationManager;
notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
String tickerText;
String expandedText;
String expandedTitle;
int icon;
long when;
Notification notification;
int notificationref = new Random().nextInt(100) + 1;
icon = R.drawable.reminder;
tickerText = "New Reminder";
when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
expandedText = "Reminder at: "
+ DateOrTimeString.getTimeString(task.time) + "\n"
+ task.detail;
expandedTitle = "Reminder:" + task.topic;
Intent intentDestroyer = new Intent(context, RemindHomeActivity.class);
intentDestroyer.putExtra("ID", task.id);
intentDestroyer.putExtra("NOTIFICATIONREF", notificationref);
launchIntent = PendingIntent.getActivity(context, notificationref,
intentDestroyer, 0);
notification.setLatestEventInfo(mContext, expandedTitle, expandedText,
null);
notificationManager.notify(1, notification);
One more thing this problem is only in API level less than 11. it is wortking in API level 15 Icecream Sandwitch
You need to set the contentIntent
void android.app.Notification.setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)
in your case:
notification.setLatestEventInfo(mContext, expandedTitle, expandedText,
launchIntent );