Cannot Resolve Method setLatestEventInfo
Well below is a simple example of working with Notifications, go through it, hope it helps!
MainActivity.java
public class MainActivity extends ActionBarActivity {
Button btnShow, btnClear;
NotificationManager manager;
Notification myNotication;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialise();
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//API level 11
Intent intent = new Intent("com.rj.notitfications.SECACTIVITY");
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setAutoCancel(false);
builder.setTicker("this is ticker text");
builder.setContentTitle("WhatsApp Notification");
builder.setContentText("You have a new message");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setSubText("This is subtext..."); //API level 16
builder.setNumber(100);
builder.build();
myNotication = builder.getNotification();
manager.notify(11, myNotication);
/*
//API level 8
Notification myNotification8 = new Notification(R.drawable.ic_launcher, "this is ticker text 8", System.currentTimeMillis());
Intent intent2 = new Intent(MainActivity.this, SecActivity.class);
PendingIntent pendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 2, intent2, 0);
myNotification8.setLatestEventInfo(getApplicationContext(), "API level 8", "this is api 8 msg", pendingIntent2);
manager.notify(11, myNotification8);
*/
}
});
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
manager.cancel(11);
}
});
}
private void initialise() {
btnShow = (Button) findViewById(R.id.btnShowNotification);
btnClear = (Button) findViewById(R.id.btnClearNotification);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btnShowNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Notification" />
<Button
android:id="@+id/btnClearNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear Notification" />
</LinearLayout>
And the activity that will be opened on click of Notification,
public class SecActivity extends Activity {
}
cannot resolve method `setLatestEventInfo`
'setLatestInfo` was removed in API 23: https://developer.android.com/sdk/api_diff/23/changes/android.app.Notification.html
You should use the Notification.Builder
instead: https://developer.android.com/reference/android/app/Notification.Builder.html
There is an example here.
I am assuming you're using an outdated Android tutorial. Try to use the ones from https://developer.android.com. They're the most reliable.
Cannot resolve setLatestEventInfo method
I have tried the below, however, it says that it cannot resolve symbol contentintent.
That is because you failed to copy some lines from your original code into your revised code:
Intent notificationIntent = new Intent(this,StartActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Have those in your revised code before you start configuring the Notification.Builder
.
setLatestEventInfo() can not resolve in Android Studio
As see here setLatestEventInfo :
setLatestEventInfo method is removed from Notification class
To create Notification
use Notification.Builder class as:
Notification.Builder builder = new Notification.Builder(MyRemiderService.this);
.....
builder.setSmallIcon(R.drawable. notification_template_icon_bg)
.setContentTitle("ContentTitle")
.....
.setContentIntent(pendingNotificationIntent);
Notification notification = builder.getNotification();
notificationManager.notify(R.drawable.notification_template_icon_bg, notification);
Related Topics
Android Expandablelistview - Looking for a Tutorial
How to Display a Route Between Two Geocoords in Google Maps
How to Find the Logs on Android Studio
How to Handle Multiple Countdown Timers in Listview
Adb Server Is Out of Date. Killing
How to Get Current Date in Android
How to Set Cursor Position in Edittext
Cannot Resolve Symbol 'Theme' in Styles.Xml (Android Studio)
Collapsingtoolbarlayout Subtitle
Get My Wifi Ip Address Android
Connecting to Wifi Using Adb Shell
Android: Open Activity Without Save into the Stack
Android: Asynctask to Make an Http Get Request
How to Hide Underbar in Edittext
Android Room: Insert Relation Entities Using Room
How to Set "Android:Layout_Below" at Runtime Programmatically