如何发送通知当特定的 Wifi BSSID(在共享首选项中)移动设备与 wifi BSSID 连接并提供服务时?

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

我的共享偏好中有一个 BBSID。每当用户连接到 Wifi 网络时,我的服务都必须检查它是否与存储在 SharedPreferences 中的 BBSID 相同。我已经实现了下面的代码,但问题是当连接到 wifi 网络时会多次创建通知。

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

}

这一行可能是错误的:

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

使用常量 id :

nm.notify(12345, notification);

并在创建新的时取消它:

notificationManager.cancel(12345)
PushNotification(context);

我还建议您 link 以更有效地过滤 ssid 更改:Android WIFI How To Detect When Specific WIFI Connection is Available