I want to create a sample project to show notification on click of button then when the user selects it open that activity in my app or open a url on selection of that. I have done something but I'm unable to complete the functionality.
First I am getting error to use this: @SuppressLint("NewApi")
If I am not using this I am getting the error on here
Notification noti = new Notification.Builder(this)
Activity Code
public class NotificationExample extends Activity implements OnClickListener{
private Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_example);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(this);
}
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this)
.setTicker("Ticker Title")
.setContentTitle("Content Title")
.setContentText("Notification content.")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent).getNotification();
noti.flags=Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
}
}
Why I have to use this:
`@SuppressLint("NewApi")`
in my code? I am not getting the notification sound. Please suggest me what changes I have to make in my code.