cannot Resolve Method setLatestEventInfo in android notification - android

I just imported an android project and in the Notification section, I have to use setLatestEventInfo. But Android Studio Says cannot resolve method setLatestEventinfo. Here is my code snippet, please kindly help edit my code while answering
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,"CEPF Mobile",0);
Intent notify_intent = new Intent(context,SpecialSerivceReportActivity.class);
notify_intent.putExtra("filename", "Special Service Report");
notify_intent.putExtra("URL", urls[0]);
PendingIntent contentIntent = PendingIntent.getActivity(context,(int)System.currentTimeMillis(),notify_intent,PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, "CEPF Mobile", "New post in Special Service Report", contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_SOUND ;
nm.notify((int)System.currentTimeMillis(),notification);
}
else if(message.equalsIgnoreCase("Special Columnist Blog"))
{
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,"CEPF Mobile",0);
Intent notify_intent = new Intent(context,SpecialSerivceReportActivity.class);
notify_intent.putExtra("filename", "Special Columnist Blog");
notify_intent.putExtra("URL", urls[1]);
PendingIntent contentIntent = PendingIntent.getActivity(context,(int)System.currentTimeMillis(),notify_intent,PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, "CEPF Mobile", "New post in Special Columnist Blog", contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_SOUND ;
nm.notify((int)System.currentTimeMillis(),notification);
}

setLatestEventInfo() has been removed from Notification in API 23. You'll need to rewrite your code if you target API 23 or higher.
Instead of:
Notification notification = new Notification(R.drawable.ic_launcher,"CEPF Mobile",0);
notification.setLatestEventInfo(context, "CEPF Mobile", "New post in Special Columnist Blog", contentIntent);
Do:
Notification.Builder builder = new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("CEPF Mobile")
.setContentText("New post in Special Columnist Blog")
.setContentIntent(contentIntent);
Notification notification = builder.buid();
A few notes on usage:
If you use the Android Support Library, use NotificationCompat.Builder instead of Notification.Builder.
Since you're setting a couple more attributes on your notifications, look at the methods offered by the builder class, and consider using these, especially if you create similar notifications in different places.
Using the app's main icon for a notification is not recommended, as Android now forces notification icons to be rendered in black and white, with all opaque areas rendered as white. You'll probably want to design an icon that is still recognizable in this setting.

For the Best Approach, Create Notification codes from the support of android studio,
Right click on app and click New then UI Components then Notification then click finish.
The code is ready to be used.

Related

Android - How can i set Counter for more than one notification in Android with only one icon?

I have been posted my code here please give me a solution for set notification counter with single icon on notification bar insted of more than one notification.
this.getApplicationContext();
mManager = (NotificationManager) this
.getApplicationContext()
.getSystemService(
Context.NOTIFICATION_SERVICE);
Intent intent1 = new Intent(
this.getApplicationContext(),
Activity_Mentor.class);
// #SuppressWarnings("deprecation")
Notification notification = new Notification(
R.drawable.dpu_ic_launcher,
"New Message",
System.currentTimeMillis());
notification.number=notificationCount++;
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent
.getActivity(
this.getApplicationContext(),
0,
intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(
this.getApplicationContext(),
getStaffName, chatMessage,
pendingNotificationIntent);
int NOTIFY_ME_ID = Integer.parseInt(chatId);
mManager.notify(NOTIFY_ME_ID, notification);
this is my code which has been used in my application but it did not work for set notification counter with single icon please help me solve thatr problem.
Different notifications are created because of the different NOTIFY_ME_ID passed to mManager.notify(NOTIFY_ME_ID, notification).
There can be only 1 notification displayed with 1 id ... meaning that if you pass different ids you'll get different notifications. On the other hand if you call .notify() with the same NOTIFY_ME_ID the old notification will just be updated (read replaced) with the new one.
Lastly I suggest you use NotificationCompat.Builder to build your notifications instead of the plain Notification class - that way you'll have better compatibility - both with older versions and also with Wearables.

Changing notifications from standard to custom type

Changing from a standard notification with text and a picture to more complex types with custom designed layout requires the use of RemoteViews class. And instead of using the setContentTitle(), I used setContent(remoteviews) method because of the custom view.
After changing to custom view I deleted the setContent, setSmallIcon, and setContentTitle mehods, however after I did that the notification never showed up again.
If I am using a custom view all I have to use us the setContent() method is that correct? they why does it not work with I remove the other methods?
RemoteViews remoteviews = new RemoteViews("com.test.example", R.layout.custom_notifications);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(AudioService.this)
.setContent(remoteviews)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setOngoing(true);
Well since ICS I have not really touched the Notification Builder. Even to this day I still do it the old school way when GB was more popular. Example time:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
// Setup an intent for when the user taps the notification
Intent notificationIntent = new Intent(this, SomeActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, notificationIntent, 0);
// `icona` is the icon shown in the status bar.
Notification notification = new Notification(icona,
"Ticker Text", System.currentTimeMillis());
// These flags should be self explanatory
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
// This is where you select the xml for you custm view
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.custom_notification);
notification.contentView = contentView;
notification.contentIntent = contentIntent;
// Some ID number for the OS to keep track of your notification
int HELLO_ID = 123456;
// Send the notification
mNotificationManager.notify(HELLO_ID, notification);
This should work for every release of android. Simply because Notification.Builder is a wrapper to make it easier to create status bar notifications. Also the builder calls these methods if you look at androids source code.

Clear Android's Local Notifications cordova plugin

I use Phonegap's localNotification Plugin for Android to show notifications on specific dates.
I use Cordova [2.2] & I used cordova's upgrading tutorial to modify the plugin.
The notification is displayed, but when I click on it, the application doesn't open and the notification isn't cleared.
How can I fix this?
In AlarmReceiver.java, around line 70, you will see the following lines of code:
// Construct the notification and notificationManager objects
final NotificationManager notificationMgr = (NotificationManager) systemService;
final Notification notification = new Notification(R.drawable.ic_launcher, tickerText,
System.currentTimeMillis());
final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.vibrate = new long[] { 0, 100, 200, 300 };
notification.setLatestEventInfo(context, notificationTitle, notificationSubText, contentIntent);
Add the appropriate lines to match the following:
// Construct the notification and notificationManager objects
final NotificationManager notificationMgr = (NotificationManager) systemService;
final Notification notification = new Notification(R.drawable.ic_launcher, tickerText,
System.currentTimeMillis());
Intent notificationIntent = new Intent(context, CLASS_TO_OPEN.class);
final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.vibrate = new long[] { 0, 100, 200, 300 };
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, notificationTitle, notificationSubText, contentIntent);
where CLASS_TO_OPEN is the name of the class you wish to open when the notification is pressed.
EDIT:
To clarify, in order to have the notification open an activity when it is press, you need to associate this activity with the notification object. This is done by creating an Intent, specifying the activity to open (as in NAME_OF_ACTIVITY.class) as the second parameter, and passing this Intent to a PendingIntent as the third parameter. This, in turn, is passed to the notification object via the setLatestEventInfo method.
In the code snippet above this is all done for you with the exception of specifying the activity to open, as this is going to be specific to your project. Unless you have added additional activities, a PhoneGap/Cordova project contains one activity, namely the one that opens the Cordova WebView. If you do not know or remember the name of this activity in your project, you can find it in the Package Explorer (in Eclipse) by following:
src>NAME_OF_YOUR_PACKAGE>NameOfActivity.java
To be sure this is the name of the class, open the java file with a text editor and you will see NAME_OF_ACTIVITY extends DroidGap. Replace CLASS_TO_OPEN in the above snippet with the name of your activity (must include .class file extension).

NotificationManager.notify throws IllegalArgumentException

I have a NotificationManager that successfully creates a Notification:
private void showNotification() {
Notification notification = new Notification(R.drawable.snog_icon, getString(R.string.sn_g_entering_beacon_mode_),
System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
Intent i = new Intent(this, SnogActivity.class);
i.putExtra("fromNotification", "yes");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);
notification.setLatestEventInfo(this, getString(R.string.sn_g_avalanche_buddy),
getString(R.string.beacon_mode_activated_), contentIntent);
notification.flags |= Notification.FLAG_ONGOING_EVENT; // Notification.DEFAULT_ALL
// Send the notification.
// We use a string id because it is a unique number. We use it later to cancel.
mNM.notify(R.string.service_started, notification);
}
That part works fine and it shows my notification, and the correct activity is started when I tap the notification. Later in the app I try to notify a simple notification:
Notification not = new Notification(R.drawable.snog_icon, "checker", System.currentTimeMillis());
not.flags |= Notification.DEFAULT_ALL;
mNM.notify(R.string.checker, not);
And that crashes the app in the notify() call with a IllegalArgumentException. I am supposed to use NotificationCompat.Builder according to quite some internet results, but that is not even available.
You could fix this exception, but I would rather do it right from the beginning by using the NotifcationCompat. It is available on the compatibility package, you have to add by Right click on your eclipse project > Android Tools > Add Support Library
after this, you will have the NotificationCompat available in your project...then visit the site: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
there are some great and simple examples there.
Good luck!

Android Notification intent to clear it self

I have read many examples of how to create notification messages.
What i wanted to achieve, is because the notification will be executed by a widget, i would like
the notification intent when clicked to clear it self when the user clicks on it.I do not have an activity to return to.
The notification for my purposes will just plainly notify, nothing else.
So what would be the code of an intent that just clear/cancel itself.
The code below is an activity launched by a button(button code not included) the notification will be fired up by a background service.
CharSequence title = "Hello";
CharSequence message = "Hello, Android!";
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final Notification notification = new Notification(R.drawable.icon,"A New Message!",System.currentTimeMillis());
notification.defaults=Notification.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, AndroidNotifications.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notification.setLatestEventInfo(AndroidNotifications.this, title,message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
Thanks
Check out FLAG_AUTO_CANCEL
Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user.
EDIT :
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Set the flags in notification.flags instead of notification.defaults.
Example:
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
If you're using NotificationCompat.Builder (a part of android.support.v4) then simply call its object's method setAutoCancel
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);
Some guys were reporting that setAutoCancel() did not work for them, so you may try this way as well
builder.build().flags |= Notification.FLAG_AUTO_CANCEL;
The only way I can see of doing this is to have your Notification's Intent point to a background Service. When this Service is launched, it would clear the given Notification using NotificationManager.cancel(int id). The Service would then stop itself. It's not pretty, and would not be easy to implement, but I can't find any other way of doing it.
/**
Post a notification to be shown in the status bar.
Obs.: You must save this values somewhere or even pass it as an extra through Intent to use it later
*/
notificationManager.notify(NOTIFICATION_ID, notification);
/**
Cancel a previously shown notification given the notification id you've saved before
*/
notificationmanager.cancel(NOTIFICATION_ID);
Using setContentIntent should solve your problem:
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
For example:
NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("title")
.setAutoCancel(true)
.setContentText("content")
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
Often you might want to direct the user to the relevant content and so might replace 'new Intent()' with something else.

Categories

Resources