Create the notification bar Notification code instance in android

  • 2020-06-19 11:44:53
  • OfStack


/////  The first 1 Step: get NotificationManager
		NotificationManager nm = (NotificationManager) 
				getSystemService(Context.NOTIFICATION_SERVICE);

		/////  The first 2 Step: define Notification
		Intent intent = new Intent(this, OtherActivity.class);
		//PendingIntent It's to be executed Intent
		PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
				PendingIntent.FLAG_CANCEL_CURRENT);
		Notification notification = new Notification.Builder(this)
				.setContentTitle("title")
				.setContentText("text")
				.setSmallIcon(R.drawable.ic_launcher).setContentIntent(pi)
				.build();
		notification.flags = Notification.FLAG_NO_CLEAR;
		
		///// The first 3 Step: Start notification bar, no 1 A parameter is 1 Only one notice 1 logo 
		nm.notify(0, notification);
		
		// Close to inform 
		//nm.cancel(0);


Related articles: