1) To create notification UI content and action we make use of NotificationCompat.Builder object and Builder object must include the following:-
- Small icon that would be shown when your notification will arrive at your device and it is done by using "
setSmallIcon()
".
- The title of your Notification is set by using "
setContentTitle()
".
- The detail text is set by using "
setContentText()
".
2) When notification is generated you need to move to a specific activity when user clicks on the notification for this w3e make use of "PendingIntent
".
3) To generate the notification
MainActivity.java
public class MainActivity extends AppCompatActivity {
private WebView mweView;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(mClickListener);
}
View.OnClickListener mClickListener=new View.OnClickListener() {
@Override
public void onClick(View v) {
notification();
}
};
private void notification()
{
Toast.makeText(MainActivity.this,"entered in Notification",Toast.LENGTH_LONG).show();
NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this).setSmallIcon(R.drawable.go).setContentTitle("WebView").setContentText("Your webView Started working");
//HomeActivity is activity where the user will be redirected when user click notification
Intent intent=new Intent(this,HomeActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
mBuilder.setAutoCancel(true);
NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,mBuilder.build());
}
}
0 Comment(s)