Je développe une application Beacon dans Android. Je veux une fonctionnalité qui lorsque l'application entre dans une région de balise alors la notification devrait venir dans l'appareil sur "région entrée" même lorsque l'application ne fonctionne pas dans android.Lorsqu'elle se termine alors "Région sort alors alors une nouvelle notification devrait venir.Je ne suis pas comment obtenir une notification dans la barre du haut quand l'application ne fonctionne pas en arrière-plan Aussi quand je clique sur la notification il devrait me conduire à l'écran de la balise S'il vous plaît aidez-moi je suis nouveau à this. Merci d'avance Voici mon codeNotification dans l'application de la balise android
public class NotifyDemoActivity extends BaseActivity {
private static final String TAG =NotifyDemoActivity.class.getSimpleName();
private static final int NOTIFICATION_ID = 123;
private BeaconManager beaconManager;
private NotificationManager notificationManager;
private Region region;
@Override protected int getLayoutResId() {
return R.layout.notify_demo;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Beacon beacon = getIntent().getParcelableExtra(ListBeaconsActivity.EXTRAS_BEACON);
region = new Region("regionId", beacon.getProximityUUID(), beacon.getMajor(), beacon.getMinor());
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
beaconManager = new BeaconManager(this);
// Default values are 5s of scanning and 25s of waiting time to save CPU cycles.
// In order for this demo to be more responsive and immediate we lower down those values.
beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(3), 0);
beaconManager.setMonitoringListener(new MonitoringListener() {
@Override
public void onEnteredRegion(Region region, List<Beacon> beacons) {
postNotification("Entered region");
Log.d(TAG, "entered");
}
@Override
public void onExitedRegion(Region region) {
postNotification("Exited region");
Log.d(TAG, "exited");
}
});
}
@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() {
super.onDestroy();
/* notificationManager.cancel(NOTIFICATION_ID);*/
Log.d(TAG, "Beacons monitoring service destroyed");
Toast.makeText(this, "Beacons monitoring service done", Toast.LENGTH_SHORT).show();
Notification noti = new Notification.Builder(NotifyDemoActivity.this)
.setContentTitle("Stopped")
.setContentText("See you!")
.setSmallIcon(R.drawable.variance_new_logo)
.build();
/*
beaconManager.disconnect();
super.onDestroy();
*/}
private void postNotification(String msg) {
Intent notifyIntent = new Intent(NotifyDemoActivity.this,DistanceBeaconActivity.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("Beacon Found")
.setContentText(msg)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.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);
}
}
Salut merci @dhaval pour la réponse. Je veux demander ce que vous entendez par "Donc, vous devrez déplacer la partie de numérisation BeaconManager dans le récepteur". Signifie si c'est une autre activité ou classe? Je sais que c'est peut-être une question stupide à poser mais s'il vous plaît guide moi! –
Fondamentalement 'Activity' est un écran, donc votre code fonctionne bien tant que l'écran est visible par l'utilisateur. Mais comme les notifications Beacon peuvent arriver à tout moment, vous devrez déplacer la partie d'écoute dans BackgroundReceiver, qui est comme une tâche de fond exécutée par Android, que l'utilisateur utilise ou non l'application. La démo de Github que j'ai mentionnée dans la réponse fait exactement cela, si vous parcourez simplement le code, vous comprendrez la logique. Si vous êtes nouveau sur Android, je suggérerais d'aller à travers la compréhension de base de l'activité, BackgroundReceiver et Service, qui vous aidera à long terme. – dhaval
yhh ok signifie que je dois faire une nouvelle activité de gestionnaire de balise selon le projet de lien et là je dois étendre le récepteur de diffusion. Et le reste est le code que je dois [email protected] –