私はAndroidスタジオを使用していますが、アプリにブロードキャストを実装して、1つのアプリのみをトリガーし、登録と登録解除にLocalbroadcastmanagerを使用しています
Android Phone
APP1 APP2
My Modules
MyModulesを 1 台の Android フォンにインストールされた 2 つのアプリと統合しました。
他のAndroid携帯からAPP1にメッセージを送信すると、 APP1にメッセージが届きますが、 APP1とAPP2の両方が同時に通知をトリガーします。
私の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);
}
インテントアクション「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>
これは私の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();
}
}
}
両方のアプリが同じブロードキャストを聞いています。この問題を解決するにはどうすればよいですか?
前もって感謝します
context.getPackageName() を使用してこの問題を修正しました アクションに追加しました
BroadcastService.java
intent.setAction(context.getPackageName()+".send");
AndroidManifest.xml
<intent-filter>
<action android:name="YOUR ANDROID APP PACKAGENAME.send"/>
</intent-filter>