4

アプリが開いているときにバックグラウンドでプッシュ通知を受け取りたいです。今のところ、Estimote Demo を変更しました。アプリがフォアグラウンドにあるときに通知が表示されますが、これはあまり役に立ちません。アプリを開くとすぐに呼び出される NotifyDemoActivity クラスのコードをここに投稿します

public class NotifyDemoActivity extends Activity {
  private static final String TAG = NotifyDemoActivity.class.getSimpleName();
  private static final int NOTIFICATION_ID = 123;
  private BeaconManager beaconManager;
  private NotificationManager notificationManager;
  private Region region;
  private long[] mVibratePattern = { 0, 200, 200, 300 };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notify_demo);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    beacon.getMinor());
    region = new Region("rid", null, null, null);
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    beaconManager = new BeaconManager(this);

    beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1), 0);

    beaconManager.setMonitoringListener(new MonitoringListener() {
      @Override
      public void onEnteredRegion(Region region, List<Beacon> beacons) {
        postNotification("Entered region");
      }

      @Override
      public void onExitedRegion(Region region) {
        postNotification("Exited region");
      }
    });
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
      finish();
      return true;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  protected void onResume() {
    super.onResume();
    notificationManager.cancel(NOTIFICATION_ID);
    beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
      @Override
      public void onServiceReady() {
        try {
          beaconManager.startMonitoring(region);
        } catch (RemoteException e) {
          Log.d(TAG, "Error while starting monitoring");
        }
      }
    });
  }

  @Override
  protected void onDestroy() {
    notificationManager.cancel(NOTIFICATION_ID);
    beaconManager.disconnect();
    super.onDestroy();
  }

  private void postNotification(String msg) {
    Intent notifyIntent = new Intent(NotifyDemoActivity.this, NotifyDemoActivity.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivities(
      NotifyDemoActivity.this,
      0,
      new Intent[]{notifyIntent},
      PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(NotifyDemoActivity.this)
    .setSmallIcon(R.drawable.beacon_gray)
    .setContentTitle("Notify Demo")
    .setContentText(msg)
    .setAutoCancel(true)
    .setContentIntent(pendingIntent)
    .setVibrate(mVibratePattern)
    .build();
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_LIGHTS;
    notificationManager.notify(NOTIFICATION_ID, notification);

    TextView statusTextView = (TextView) findViewById(R.id.status);
    statusTextView.setText(msg);
  }
}
4

2 に答える 2

3

BeaconManagerアクティビティではなく、アプリケーション クラスで保持する必要があります。

アクティビティは停止、破棄され、BeaconManager監視が停止されます。一方、アプリケーションは引き続き参照を保持し、監視を続けます。

アプリケーションクラスの監視中にビーコンが見つかった場合、通知を投稿できます。ユーザーがタップすることを決定すると、いくつかのアクティビティがトリガーされます。

于 2014-05-27T21:53:57.217 に答える
0

サービスを作成する必要があります。

コードでいくつかの変更を行う必要があります。

beaconManager = new BeaconManager(this);

「ビーコンサービス」を開始する必要があります

      if (!beaconManager.isBluetoothEnabled()) {
          stopSelf();
      }

          beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
              @Override
              public void onServiceReady() {
                try {
                  beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);
                } catch (RemoteException e) {
                  Log.e("error", "Cannot start ranging", e);
                }
              }
          });

デバイスで Bluetooth が有効になっているかどうかも確認してください。

手順 beaconManager.setMonitoringListener(new MonitoringListener() で、通知を作成するコマンドを追加します。

于 2014-08-23T16:26:47.050 に答える