I found this method on the vogella website. they called this method from the onclick attribute of the button in the main.xml file. Can anyone tell how to change this method to use without calling View?
public void createNotification(View view) {
Intent intent = new Intent(this, Home.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle("title")
.setContentText("content").setSmallIcon(R.drawable.original_logo)
.setContentIntent(pIntent)
.addAction(R.drawable.original_logo, "Call", pIntent)
.addAction(R.drawable.original_logo, "More", pIntent)
.addAction(R.drawable.original_logo, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
This is the link for the place where I got this method
http://www.vogella.com/tutorials/AndroidNotifications/article.html
I tried this way. This is the edited version of the method above.
public void createNotification(String title,String content) {
Intent intent = new Intent(this, Home.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(content).setSmallIcon(R.drawable.original_logo)`enter code here`
.setContentIntent(pIntent)
.addAction(R.drawable.original_logo, "Call", pIntent)
.addAction(R.drawable.original_logo, "More", pIntent)
.addAction(R.drawable.original_logo, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
Then I tried to call this edited method, as shown below.
try {
Double inc_val = Double.parseDouble(display_incamo.getText().toString());
Double exp_val = Double.parseDouble(display_expamo.getText().toString());
if(inc_val<exp_val){
createNotification("Expenses are High","Your expenses are almost higher than income");
}else{
createNotification("Expenses are Low","Keep it up Buddy!!!");
}
}catch(Exception e){
e.printStackTrace();
}
But then the notification does not appear.