0

現在、Azure Notification Hubを使用してリモート通知を受信するためのアプリケーション設定があります。 ここで、iBeaconsをスキャンして特定のものが近くにあるかどうかを確認したい場合は、通知をユーザーに表示しないでください。ただし、ビーコンが見えない場合は、この通知を受け取る必要があります。リモート通知を受信するとiBeaconをスキャンします

基本的に私はビーコンにこのアプリの通知を控えて欲しいです。

これを行うにはどうすればいいですか?

答えて

0

いくつかのopensourceライブラリを使用してビーコンを操作できます。私はAltbeaconライブラリを使用しました。 ここにサンプルがあります。https://altbeacon.github.io/android-beacon-library/samples.html ターゲットには、アクティビティまたはサービスでBeaconConsumerインターフェイスを実装する必要があります。それはメソッドonBeaconServiceConnect()を持っています。実装の例:あなたがベースの通知をフィルタリングする場合

public class MyHandler extends NotificationsHandler { 
    public static final int NOTIFICATION_ID = 1; 
    private NotificationManager mNotificationManager; 
    NotificationCompat.Builder builder; 
    Context ctx; 

    @Override 
    public void onReceive(Context context, Bundle bundle) { 
    ctx = context; 
    String nhMessage = bundle.getString("message"); 
    sendNotification(nhMessage); 
    if (MainActivity.isVisible) { 
     MainActivity.mainActivity.ToastNotify(nhMessage); 
    } 
    } 

    private void sendNotification(String msg) { 
    // put your notification code here 
    ... 
    } 

}

:リモート通知が入ってきたとき、あなたはこのようにコールバックを取得the docs from Azureに基づいて

@Override 
    public void onBeaconServiceConnect() { 
     beaconManager.addRangeNotifier(new RangeNotifier() { 
      @Override 
      public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { 
       if (beacons.size() == 0) { 
        Log.i(TAG, "Show your notification here");   
       } 
      } 
     }); 

     try { 
      beaconManager.startRangingBeaconsInRegion(new Region("someRangingUniqueId", null, null, null)); 
     } catch (RemoteException e) { } 
    } 
1

、どのようなビーコンが存在する場合は、このロジックをonReceiveメソッドに次のように追加できます。

public void onReceive(Context context, Bundle bundle) { 
    if (!(MyApplication)this.getApplication()).isBeaconVisible()) { 
     // Suppress notification by returning early from method 
     return; 
    } 
    ... 
    } 

上記のisBeaconVisible()は、Android Beacon Libraryを使用してカスタムAndroidアプリケーションクラスに実装できます。この作業をするためにライブラリを設定する方法については、さらに詳しく読む必要があります。また、カスタムApplicationクラスをAndroidManifest.xmlに登録する必要があります。任意の識別子を有する任意のビーコンが最後秒間に見られた場合isBeaconVisible()ため

public class MyApplication extends Application implements BeaconConsumer, RangeNotifier { 

    public Collection<Beacon> mVisibleBeacons; 

    public void onCreate() { 
     super.onCreate(); 
     BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this); 
     // TODO: look up the proper I_BEACON_LAYOUT in a google search 
     beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(I_BEACON_LAYOUT)); 
     beaconManager.addRangeNotifier(this); 

    } 

    @Override 
    public void onBeaconServiceConnect() { 
     BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this); 
     try { 
      beaconManager.startRangingBeaconsInRegion(new Region("all-beacons", null, null, null)); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { 
     mVisibleBeacons = beacons; 
    } 

    public boolean isBeaconVisible() { 
     return mVisibleBeacons.size() > 0; 
    } 
} 

上記論理が真を返します。しかし、これを変更して、要件ごとにより洗練されたものにすることができます。

関連する問題