I am using android studio and i struck in implementing broadcast in my app so that it should trigger only one App and i'm using Localbroadcastmanager for registering and unregistering
Android Phone
APP1 APP2
My Modules
I have integrated MyModules with two apps installed in one Android phone.
When i send a message to APP1 from some other Android phone it receives message to APP1 But both APP1 and APP2 triggers the notification at a same time.
My BroadcastService.java
public static void sendNotificationBroadcast(Context context, Message message) {
Log.i(TAG, "Sending notification broadcast...");
Intent notificationIntent = new Intent();
notificationIntent.putExtra(MyConstants.MESSAGE_JSON_INTENT, GsonUtils.getJsonFromObject(message, Message.class));
notificationIntent.setAction("com.notification.send");
context.sendBroadcast(notificationIntent);
}
i'm setting my intent action "com.notification.send"
AndroidManifest.xml
<receiver android:name="com.notification.NotificationBroadcastReceiver" android:exported="false">
<intent-filter>
<action android:name="com.notification.send"/>
</intent-filter>
</receiver>
This is my NotificationBroadcastReceiver.java
public class NotificationBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "NotificationBroadcastReceiver";
private static String NOTIFICATION_ICON_METADATA = "com.notification.icon";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Integer notificationIconId = Utils.getMetaDataValueForResources(context, NOTIFICATION_ICON_METADATA);
String messageJson = intent.getStringExtra(Constants.MESSAGE_JSON_INTENT);
Log.i(TAG, "Received broadcast, action: " + action + ", message: " + messageJson);
if (!TextUtils.isEmpty(messageJson)) {
final Message message = (Message) GsonUtils.getObjectFromJson(messageJson, Message.class);
final NotificationService notificationService =
new NotificationService(notificationIconId == null ? R.drawable.ic_launcher : notificationIconId, context, R.string.wearable_action_label, R.string.wearable_action_title, R.drawable.ic_action_send);
final Contact contact = new contactService(context).getContactById(message.getIds());
new Thread(new Runnable() {
@Override
public void run() {
notificationService.notifyUser(contact, message);
}
}).start();
}
}
}
both Apps are listening to same broadcast, How to solve this problem ?
Thanks in advance
i fixed this problem by using context.getPackageName() i have appended with my action
BroadcastService.java
intent.setAction(context.getPackageName()+".send");
AndroidManifest.xml
<intent-filter>
<action android:name="YOUR ANDROID APP PACKAGENAME.send"/>
</intent-filter>