Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Cancel Notification after click on Action (Like Call)

I have an action to Dial a number via

uri = Uri.parse("tel:" + address);
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(uri);
PendingIntent pd = PendingIntent.getActivity(context, 0,intent, 
       PendingIntent.FLAG_UPDATE_CURRENT);
notif.addAction(R.drawable.ic_menu_call, "Call", pd);

but the problem is that I don't know

how/when to call the NotificationManager's manager.cancel() function

so as to dismiss the notification when the call action is clicked!

like image 823
Daksh Avatar asked Nov 03 '22 07:11

Daksh


1 Answers

I had the same situation and I managed to solve it by creating a broadcast receiver that is called when the action button is pressed. The broadcast receiver then receives an intent with the notification id that you want to dismiss and the number you want to dial.

The is the code that creates the notification:

NotificationManager notificationManager =
  (NotificationManager)MyApplication.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
//for some versions of android you may need to create a channel with the id you want
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel chan = new NotificationChannel("your_channel_id", "ChannelName", NotificationManager.IMPORTANCE_DEFAULT);
    notificationManager.createNotificationChannel(chan);
}
Intent intent = new Intent(MyApplication.getAppContext(), ActionReciever.class);
intent.putExtra("phoNo", phoneNumber);

// num is the notification id
intent.putExtra("id", num);

PendingIntent myPendingIntent = PendingIntent.getBroadcast(
                  MyApplication.getAppContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT
                );
Notification n = new NotificationCompat.Builder(MyApplication.getAppContext(),
                        "your_channel_id")
                        .setSmallIcon(R.drawable.app_pic)
                        .addAction(R.drawable.app_pic, "Dial now", myPendingIntent)
                        .setAutoCancel(true)
                        .build();
notificationManager.notify(num, n);

This is the broadcast receiver code, it is called when the action button is pressed. The received intent here is the intent inside the pending intent we prepared in the notification:

public class ActionReciever extends BroadcastReceiver {
    @SuppressLint("MissingPermission")
    @Override
    public void onReceive(Context context, Intent intent) {
        String phoneNumber = intent.getStringExtra("phoNo");
        int id = intent.getIntExtra("id",0);
        Intent i = new Intent(Intent.ACTION_DIAL);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setData(Uri.parse("tel:" + phoneNumber));
        NotificationManager notificationManager =
                (NotificationManager) MyApplication.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(id);
        context.startActivity(i);
    }
}

Register the BroadcastReceiver in the app manifest inside application tag

<receiver android:name=".ActionReciever" />

MyApplication is a class that extends the default Application so I can have a place to store the context I need.

public class MyApplication extends Application {
    private static Context context;

    public void onCreate() {
        super.onCreate();
        MyApplication.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return MyApplication.context;
    }

}

Note that you need to update the manifest to run the MyApplication class like this:

android:name="com.example.yourpackage.MyApplication"

This code works even if the app is down and without a background service.

like image 142
harelon Avatar answered Nov 11 '22 09:11

harelon