Search code examples
androidandroid-wifiwifimanager

How to send a notification When the particular Wifi BBSID (in Shared Preference )mobile connected with wifi BBSID with service?


I have one BBSID in my shared preference. Whenever a user connect to a Wifi network my service have to check if it is the same BBSID as the one stored in SharedPreferences. I have implemented below code but the problem is that the Notification is created multiple times when connected to a wifi network .

public class WifiConnectionNotificationService extends Service {
BroadcastReceiver mReceiver;
String bssid = "64:66:b3:f0:ce:81";
/*
WifiConnectionNotificationService(String ssid) {
    this.ssid = ssid;
}*/

// use this as an inner class like here or as a top-level class
public class IsWifiConnectedBroadcastReceiver extends BroadcastReceiver {

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        if (intent.getAction().contentEquals("android.net.wifi.STATE_CHANGE") || intent.getAction().contentEquals("android.net.conn.CONNECTIVITY_CHANGE")) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

            if (wifiManager.isWifiEnabled()) {
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
                    if (wifiInfo.getBSSID().contentEquals(bssid)) {
                        PushNotification(context);
                    } else {
                        if (Context.NOTIFICATION_SERVICE != null) {
                            notificationManager.cancelAll();
                        }
                    }
                }
            } else {
                if (Context.NOTIFICATION_SERVICE != null) {
                    notificationManager.cancelAll();
                }
            }
        }
    }
}

@Override
public void onCreate() {
    // get an instance of the receiver in your service
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.net.wifi.STATE_CHANGE");
    filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    mReceiver = new IsWifiConnectedBroadcastReceiver();
    registerReceiver(mReceiver, filter);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void PushNotification(Context context) {
    NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(context);
    Intent notificationIntent = new Intent(context, Register.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    //set
    builder.setContentIntent(contentIntent);
    builder.setSmallIcon(android.R.drawable.ic_notification_clear_all);
    builder.setContentText("Do you want me to start your Bike ?");
    builder.setContentTitle("Keyless Moto");
    builder.setAutoCancel(true);
    builder.setDefaults(Notification.DEFAULT_ALL);

    Notification notification = builder.build();
    nm.notify((int) System.currentTimeMillis(), notification);
}

}


Solution

  • This line is probably wrong:

    nm.notify((int) System.currentTimeMillis(), notification);
    

    Use a constant id :

    nm.notify(12345, notification);
    

    And cancel it when a new one is created:

    notificationManager.cancel(12345)
    PushNotification(context);
    

    I also suggest you this link to filter more efficiently ssid changes : Android WIFI How To Detect When Specific WIFI Connection is Available