Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
123 views
Welcome To Ask or Share your Answers For Others

1 Answer

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...