I receive this error when compiling a project: error: can not find symbol method setLatestEventInfo

1

This is error 1:

  error: cannot find symbol method 
  setLatestEventInfo(MySimpleNotification,CharSequence,
  CharSequence,PendingIntent)

Part of the code of that error

      public class MySimpleNotification extends Activity{
       DatabaseConnection connection;
      @Override
          protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.my_simple_notification);
    connection=new DatabaseConnection(this);

    int notifID = getIntent().getExtras().getInt("NotifID");
    //int res=(int)connection.saveToTransaction(notifID); 
    //---PendingIntent to launch activity if the user selects 
    // the notification---
    Intent i = new Intent(MySimpleNotification.this,TransactionNotification.class);
    i.putExtra("NotifID", notifID);
    //i.putExtra("rowID", res);

    PendingIntent detailsIntent = 
        PendingIntent.getActivity(MySimpleNotification.this, 0, i, 0);

    NotificationManager nm = (NotificationManager)
        getSystemService(NOTIFICATION_SERVICE);
    Notification notif = new Notification(
        R.drawable.icon, 
        "New Transaction",
        System.currentTimeMillis());

    CharSequence from = "Daily expense :: Transaction";
    CharSequence message = "New Transaction is saved";

  ***AQUÍ ME MARCA EL ERROR--> notif.setLatestEventInfo(this, from, message, 
   detailsIntent);

    //---100ms delay, vibrate for 250ms, pause for 100 ms and
    // then vibrate for 500ms---
    notif.vibrate = new long[] { 100, 250, 100, 500};        
    nm.notify(notifID, notif);
    finish();
}
@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();

}

Error 2:

           error: cannot find symbol method 
          setLatestEventInfo(Context,String,String,PendingIntent)

Bit of second error code

     public class NotifyService extends Service{

final static String ACTION = "NotifyServiceAction";
final static String STOP_SERVICE = "";
final static int RQS_STOP_SERVICE = 1;
static int count=0;
NotifyServiceReceiver notifyServiceReceiver;

private static final int MY_NOTIFICATION_ID=1;
private NotificationManager notificationManager;
private Notification myNotification;
@Override
public void onCreate() {
// TODO Auto-generated method stub
//notifyServiceReceiver = new NotifyServiceReceiver();
super.onCreate();


}

@SuppressWarnings("deprecation")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub

//IntentFilter intentFilter = new IntentFilter();
//intentFilter.addAction(ACTION);
//registerReceiver(notifyServiceReceiver, intentFilter);

// Send Notification


    notificationManager =
             (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            myNotification = new Notification(R.drawable.icon,
              "Notification!",
              System.currentTimeMillis());
            Context context = getApplicationContext();
            String notificationTitle = "New Transaction";
            String notificationText = "new transaction occurs";
            Intent myIntent = new Intent(this, MySimpleNotification.class);
            PendingIntent pendingIntent
              = PendingIntent.getActivity(getBaseContext(),
                0, myIntent,PendingIntent.FLAG_CANCEL_CURRENT);
            myNotification.defaults |= Notification.DEFAULT_SOUND;
            myNotification.flags |= Notification.FLAG_AUTO_CANCEL;

          AQUÍ ME MARCA EL ERROR->myNotification.setLatestEventInfo(context,
               notificationTitle,
               notificationText,
               pendingIntent);
            //myNotification.number++;
            notificationManager.notify((int)System.currentTimeMillis(), 
   myNotification);
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
this.unregisterReceiver(notifyServiceReceiver);
super.onDestroy();
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

public class NotifyServiceReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
 // TODO Auto-generated method stub
 int rqs = arg1.getIntExtra("RQS", 0);
 if (rqs == RQS_STOP_SERVICE){
  stopSelf();
 }
}
}
    
asked by user3802101 27.04.2018 в 04:49
source

2 answers

0

As you can see here setLatestEventInfo:

The setLatestEventInfo method has been removed from the Notification class.

To create a notification use the Notification.Builder class as follows:

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);

Response obtained from: link

    
answered by 27.04.2018 в 08:33
-1

The Notification.setLatestEventInfo() method was removed in API 23 if your project is configured with a target greater than this API will not work, in fact the best option is to change the way to create the notification and not lower the level of the API.

Review the changes for API 23, Android 6.0

  

Notifications In this release, the Notification.setLatestEventInfo() method is removed. Use the Notification.Builder class,   instead of creating notifications. To update a notification of   repeated way, reuses the instance of Notification.Builder . Call   to the build() method to get Notification instances   updated.

You can use in addition to the NotificationCompat class instead of Notification for compatibility between versions.

I add a method to create Notifications:

   private void sendNotification(String message) {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("My notification")
                        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                        .setContentText(message);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(001, mBuilder.build());
    }

and a more complete example of what you can do with notifications :

    
answered by 27.04.2018 в 16:52