Reputation: 3
I have specified 2 proximity alerts for 2 different points. For each proximity alert i have specified an eventID as you can see in the following code. For each proximity alert(for each point) i want differrent notification sound. The main question is how i can create different notifications (notifications sound) within the same class.
Here is the code i have used.
private void setProximityAlert(double lat, double lon, final int eventID,int requestCode) {
float radius = 10f;
long expiration =-1;
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Bundle extras = new Bundle();
Intent intent = new Intent(PROXIMITY_INTENT_ACTION);
intent.putExtra(ProximityAlert.EVENT_ID_INTENT_EXTRA, eventID);
intent.putExtra(PROXIMITY_INTENT_ACTION, extras);
PendingIntent proximityIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode , intent, PendingIntent.FLAG_CANCEL_CURRENT);
locManager.addProximityAlert(
lat,
lon,
radius,
expiration,
proximityIntent
);
}
private void initialiseReceiver()
{
IntentFilter filter = new IntentFilter(PROXIMITY_INTENT_ACTION);
registerReceiver(new ProximityAlert(), filter);
}
In the BroadcastReceiver class i have the notification in the following code
public class ProximityAlert extends BroadcastReceiver {
public static final String EVENT_ID_INTENT_EXTRA = "EventIDIntentExtraKey";
@Override
public void onReceive(Context context, Intent intent) {
int eventID = intent.getIntExtra(EVENT_ID_INTENT_EXTRA, 1);
switch(+eventID) {
case '1':
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
Log.d(getClass().getSimpleName(), "entering");
}
else {
Log.d(getClass().getSimpleName(), "exiting");
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);
Notification notification = new Notification();
notification.setLatestEventInfo(context, "Proximity Alert!", "ειναι σωστο αυτο που εκανεσ", pendingIntent);
notificationManager.notify(1, notification);
notification.icon = R.drawable.ic_menu_notifications;
notification.sound = Uri.parse("file:///sdcard/File/sdcard/introduction_file.mp3");
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_INSISTENT;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.ledARGB = Color.WHITE;
notification.ledOnMS = 1500;
notification.ledOffMS = 1500;
break;
case '2':
String key1 = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering1 = intent.getBooleanExtra(key1, false);
if (entering1) {
Log.d(getClass().getSimpleName(), "entering");
}
else {
Log.d(getClass().getSimpleName(), "exiting");
}
NotificationManager nnotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, null, 0);
Notification notification1 = new Notification();
notification1.setLatestEventInfo(context, "Proximity Alert!", "ειναι σωστο αυτο που εκανεσ", pendingIntent1);
nnotificationManager.notify(2, notification1);
notification1.icon = R.drawable.ic_menu_notifications;
notification1.sound = Uri.parse("file:///sdcard/File/sdcard/Carter_Lane.mp3");
notification1.flags |= Notification.FLAG_AUTO_CANCEL;
notification1.flags |= Notification.FLAG_INSISTENT;
notification1.flags |= Notification.FLAG_ONGOING_EVENT;
notification1.ledARGB = Color.WHITE;
notification1.ledOnMS = 1500;
notification1.ledOffMS = 1500;
break;
I have used the switch method so, for different eventID the programme should change notification. but it does not work. can you help me thank you very much.
Upvotes: 0
Views: 356
Reputation: 1006869
You only have one PendingIntent
.
Quoting the documentation:
If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid
Since you have the same operation (getBroadcast()
) and the same Intent
routing pieces each time, there is only one PendingIntent
.
Rather than setting the action to be PROXIMITY_INTENT_ACTION
, make it be unique for each distinct proximity alert. If you use the component constructor on Intent
(i.e., new Intent(this, MyReceiver.class)
), the different action strings will not impact the routing of the broadcast.
Upvotes: 1