Show change notification method in android?

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);

    // Build notification
    // Actions are just fake
    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);
    // hide the notification after its selected
    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) {
    // Prepare intent which is triggered if the
    // notification is selected
    Intent intent = new Intent(this, Home.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

    // Build notification
    // Actions are just fake
    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);
    // hide the notification after its selected
    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.

+4
source share
2 answers

Try the following:

public void createNotification(View view) {

  showNotification("hello");
} 

public void showNotification(String msg)
{
  Intent intent = new Intent(this, Home.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

    // Build notification
    // Actions are just fake
    Notification noti = new Notification.Builder(this)
            .setContentTitle("title")
            .setContentText(msg).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);
    // hide the notification after its selected
    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, noti);

}
+1
source

, , , View , Vogella,

, , , , , clickListner, , (android:onClick="createNotification")

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:onClick="createNotification"
    android:text="Create Notification" >
</Button>

, View .

, . , android:onClick="createNotification"

Button my_button = (Button)findViewById(R.id.button1);
my_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Now call any method which you want here or
            makeNotification();
        }
    });

makeNotification(){

 Intent intent = new Intent(this, Home.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

// Build notification
// Actions are just fake
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);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(0, noti);

}
0

Source: https://habr.com/ru/post/1622207/


All Articles