0

I am trying to replace the built in calendar reminder's notification with a custom built notification using the NotificationManager.

I have added a BroadCastReceiver, that intercepts the calendar reminder's notification, as such:

     <receiver
        android:name=".receivers.CalendarNotificationBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.EVENT_REMINDER"/>
            <data android:scheme="content"/>
            <data android:host="com.android.calendar"/>
        </intent-filter>
    </receiver>

The intent is passed on to the onReceive method as follows: enter image description here

Q1: How can I use the ContentResolver API in order to find information about the event that triggered the notification (I need the TITLE field for example for building the new notification)?

Q2: How can I cancel the intent so that the calendar event's reminder notification does not get triggered? I've tried following the recomendations in this post, but to no avail: How to cancel this repeating alarm? Also, I have tried (with no success):

PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT).cancel();

EDIT ******************

After following Ovidiu Latcu's proposal, I have provided an implementation for the NotificationListenerService that also has a BroadcastReceiver for the new notifications

AndroidManifest

<service android:name=".service.CalendarNotificationListenerService"
    android:label="@string/app_name"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>

NotificationListenerService

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
/**
 * Due to an android bug: https://stackoverflow.com/questions/17911883/cannot-get-the-notificationlistenerservice-class-to-work
 * This class' name should be changed before each debug use, because if not, onReceive will never get called.
 */
public class CalendarNotificationListenerService extends NotificationListenerService {

    private String TAG = this.getClass().getSimpleName();
    private CalendarNotificationBroadcastReceiver receiver;

@Override
public void onCreate() {
    Log.i(TAG,"********** Service is created");
    super.onCreate();
    receiver = new CalendarNotificationBroadcastReceiver();
    IntentFilter intentFilter = new IntentFilter("android.intent.action.EVENT_REMINDER");
    intentFilter.addDataScheme("content");
    registerReceiver(receiver, intentFilter);
}

@Override
public void onDestroy() {
    Log.i(TAG,"**********  Service is destroyed");
    super.onDestroy();
    unregisterReceiver(receiver);
}

@Override
public IBinder onBind(Intent mIntent) {
    IBinder mIBinder = super.onBind(mIntent);
    Log.i(TAG, "**********  onBind");
    return mIBinder;
}

@Override
public boolean onUnbind(Intent mIntent) {
    boolean mOnUnbind = super.onUnbind(mIntent);
    Log.i(TAG, "**********  onUnbind");
    return mOnUnbind;
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    Log.i(TAG,"**********  onNotificationPosted");
    Log.i(TAG,"ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());

    Notification currentNotification = sbn.getNotification();

    String notificationTitle = (String) sbn.getNotification().extras.get("android.title");

    if(notificationTitle == null || !notificationTitle.startsWith("CALLERQ:")){
        return;
    }

    //Seem to only be able to cancel a notification after it is posted.
    CalendarNotificationListenerService.this.cancelNotification(sbn.getKey());

    Intent i = new  Intent("com.enginizer.NOTIFICATION_LISTENER_EXAMPLE");
    i.putExtra("notification_event","onNotificationPosted :" + sbn.getPackageName() + "\n");
    sendBroadcast(i);
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
    Log.i(TAG,"********** onNOtificationRemoved");
    Log.i(TAG,"ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText +"\t" + sbn.getPackageName());
    Intent i = new  Intent("com.enginizer.NOTIFICATION_LISTENER_EXAMPLE");
    i.putExtra("notification_event","onNotificationRemoved :" + sbn.getPackageName() + "\n");

    sendBroadcast(i);
}


class CalendarNotificationBroadcastReceiver extends BroadcastReceiver {

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "********** Notification received");
        String intentAction = intent.getAction();
        Log.i(TAG, "********** " + intentAction);
        intent.getBundleExtra(CalendarContract.EXTRA_CUSTOM_APP_URI);

        ContentResolver contentResolver = context.getContentResolver();

        //This throws an java.lang.IllegalArgumentException 
        Cursor cursor = contentResolver.query(intent.getData(), new String[]{CalendarContract.Events.DESCRIPTION}, null, null, null);

        CalendarNotificationListenerService.this.cancelAllNotifications();
    }
}

@Override
public void onListenerConnected() {
    Log.i(TAG,"********** Listener connected");
    super.onListenerConnected();
}

}

For what was answered in Q1, I am trying to query the calendar for the event noted in the mData field. When doing so, I receive the following:

java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/1491653055193

For Q2, I am not able to cancel the notification from within the onReceive method since I was not able to find it on the intent. I am only able to cancel the notification in the onNotificationPosted method, which is later than expected.

Should it really be so tangled to cancel a calendar reminder notification and replace it with a custom one?

Community
  • 1
  • 1

1 Answers1

0

Q1: The mData field contains the URI of that event. You should probably query that URI and find all the information about the event.

Q2: Since API 18 you can use the NotificationListenerService to intercept when a notification was posted, and cancel it using cancelNotification(String key)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84