0

After Successfully deleting an SMS from Broadcast, i cant clear notification from notification drawer

1)when i open sms app, that sms is deleted, but in notification, shows sms with msg

2)when i click on sms notification, it opens 'sms app with new blank sms'

i tried to clear notification drawer by: (1)

NotificationManager notificationManager = (NotificationManager) supercontext.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();

i tried to clear notification drawer by: (2)

Intent closeIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
closeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
supercontext.startActivity(closeIntent);

But still msg is visible in Notification Drawer here is my code in Broadcast.java:

@Override
public void onReceive(Context context, Intent intent) {
    final Bundle bundle = intent.getExtras();
    storageReference = FirebaseStorage.getInstance().getReference();
    supercontext = context;
    try {
        if (bundle != null) {
            final Object[] pdusObj = (Object[]) bundle.get("pdus");
            for (int i = 0; i < pdusObj.length; i++) {
                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                senderNum = phoneNumber;
                message = currentMessage.getDisplayMessageBody();
                deleteSMSHandler()
           }

public void deleteSMSHandler() {
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            try {
                deleteSMS(supercontext, message, senderNum);
            } catch (Exception e) {
            }
        }
    }, 2000);
}

public void deleteSMS(Context context, String message, String number) {
    try {
        Uri uriSms = Uri.parse("content://sms/inbox");
        Cursor c = context.getContentResolver().query(uriSms,
                new String[]{"_id", "thread_id", "address",
                        "person", "date", "body"}, null, null, null);
        if (c != null && c.moveToFirst()) {
            do {
                long id = c.getLong(0);
                long threadId = c.getLong(1);
                String where = "address=" + number;
                where = "thread_id=" + threadId;
                String address = c.getString(2);
                String body = c.getString(5);
                if (message.equals(body) && address.equals(number)) {
                    context.getContentResolver().delete(Uri.parse("content://sms/" + id), where, null);

                    NotificationManager notificationManager = (NotificationManager) supercontext.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.cancelAll();

                    Intent closeIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
                    closeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    supercontext.startActivity(closeIntent);
                }
            } while (c.moveToNext());
        }

    } catch (Exception e) {
        Log.i("Delete MSG Error", "" + e);
        String x = e + "";
        //mLogger.logError("Could not delete SMS from inbox: " + e.getMessage());
    }
}

}

here is my manifests file:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.INTERNET" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.INTERNAL_SYSTEM_WINDOW" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.SET_ALWAYS_FINISH" />
<uses-permission android:name="android.permission.SET_ORIENTATION" />
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"/>
<uses-permission android:name="android.permission.BROADCAST_SMS"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name="com.sagarmakhija1994.Framework.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name="com.sagarmakhija1994.Framework.MyBroadcastReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter android:priority="999">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <action android:name="android.service.notification.NotificationListenerService" />
            <action android:name="android.intent.ACTION_CLOSE_SYSTEM_DIALOGS" />
        </intent-filter>
    </receiver>
</application>

Sagar Makhija
  • 863
  • 8
  • 9

1 Answers1

0

After Successfully deleting an SMS from Broadcast

You cannot delete an SMS on Android 4.4+, unless you are the user's chosen SMS client.

But still msg is visible in Notification Drawer

Correct. That Notification was raised by the SMS app. The SMS app can clear it. On Android 4.4+, your app would have to be the SMS app, and so you would cancel() your own Notification.

You could implement a NotificationListenerService and use that to forcibly clear the SMS. The user would have to agree, in Settings, to let you spy on all the user's notifications. Few users would do this. And NotificationListenerService is only available on Android 4.3+. Hence, the only version of Android for which you would get any value from it would be Android 4.3.

here is my manifests file

Please note that several of the permissions that you have listed are ones that only system apps can hold.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • You cannot delete an SMS on Android 4.4+, but i done it, above code is perfectly deleting sms. i tested in both android 5.1 and 4.4 just i want to clear notification. may b done it by fore close msg app just want to know, how i force close sms app like the greenyfy app do – Sagar Makhija Mar 26 '17 at 07:02
  • You cannot delete an SMS on Android 4.4+, but the above code is working fine it successfully deletes the sms in both android 5.1 and 4.4 i just want to clear notification of that sms when i open the sms app after deleting the sms by Reciver the notification get clear and there is no sms which i deleted by Reciver but problem is till i m not opening sms app the sms is visible and readable by user in notification drawer – Sagar Makhija Mar 26 '17 at 07:17
  • 1
    @SagarMakhija: Your device is unusual. [Only the user's chosen SMS client can write to the SMS provider starting with Android 4.4](https://android-developers.googleblog.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html). – CommonsWare Mar 26 '17 at 10:45