Issue with Notification Manager on android

拈花ヽ惹草 提交于 2019-12-08 02:06:37

问题


I'm trying to notify using a button, but both Notification and setLatestEventInfo is deprecated.

Two errors:

1.The constructor Notification(int, CharSequence, long) is deprecated Notification notify = new Notification(android.R.drawable.stat_notify_more, "Hello all", System.currentTimeMillis());

2.The method setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent) in the type Notification is not applicable for the arguments (Context, CharSequence, CharSequence, Intent) notify.setLatestEventInfo(context, title, details, intent);

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new View.OnClickListener() {   
        @Override
        public void onClick(View v) {
            NotificationManager ns = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notify = new Notification(android.R.drawable.stat_notify_more, "Hello all", System.currentTimeMillis());
            Context context = MainActivity.this;
            CharSequence title ="you have be notified";
            CharSequence details = "Continue your work";
            Intent intent = new Intent(context,MainActivity.class);
            PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
            notify.setLatestEventInfo(context, title, details, intent);
            ns.notify(0,notify);


        }
    });
}

API LEVELS:

       android:minSdkVersion="11"
       android:targetSdkVersion="17"

What is the alternative?


回答1:


1. The constructor was deprecated in api level 11. so you should use Notification.Builder.

for e.g.

Notification notification = new Notification.Builder(mContext)
     .setContentTitle("New mail from " + sender.toString())
     .setContentText(subject)
     .setSmallIcon(R.drawable.new_mail)
     .setLargeIcon(aBitmap)
     .build();

2. in your code you are passing the intent instead of pending in setLatestEventInfo

....
Intent intent = new Intent(context,MainActivity.class);
        PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
        notify.setLatestEventInfo(context, title, details, pending);
        ns.notify(0,notify);
....



回答2:


notificationManager =
    (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
   myNotification = new Notification(R.drawable.icon,
     "Notification!",
     System.currentTimeMillis());
   Context context = getApplicationContext();
   String notificationTitle = "Exercise of Notification!";
   String notificationText = "http://niravranpara.blogspot.com/";
   Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
   PendingIntent pendingIntent
     = PendingIntent.getActivity(AndroidNotification.this,
       0, myIntent,
       Intent.FLAG_ACTIVITY_NEW_TASK);
   myNotification.defaults |= Notification.DEFAULT_SOUND;
   myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
   myNotification.setLatestEventInfo(context,
      notificationTitle,
      notificationText,
      pendingIntent);
   notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

  }



回答3:


Those constructor and method has been deprecated.so you should use notification builder instead.

Notification noti = new Notification.Builder(mContext) .setContentTitle("New mail from " + sender.toString()) .setContentText(subject)
.setSmallIcon(R.drawable.new_mail) .setLargeIcon(aBitmap) .build();




回答4:


Instead of System.currentTimeMillis() try java.lang.System.currentTimeMillis()



来源:https://stackoverflow.com/questions/15493633/issue-with-notification-manager-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!