Передача данных из трансляции в активность (Pushy.me) - PullRequest
0 голосов
/ 24 сентября 2019

Я пытаюсь использовать Pushy.me.Я могу получить и проанализировать данные, но я хочу обновить пользовательский интерфейс с этими данными.Как передать данные из BroadcastReceiver в Activity?

Мой класс приемника:

public class PushReceiver extends BroadcastReceiver {

private LocalBroadcastManager broadcaster;
public static String REQUEST_ACCEPT = "REQUEST_ACCEPT";

@Override
public void onReceive(Context context, Intent intent) {
    String notificationTitle = "Title";
    String notificationText = "Text";

    /**/

    Bundle bundle = intent.getExtras();
    JSONObject jsonObject = new JSONObject();

    for (String key : bundle.keySet()) {
        Object obj = bundle.get(key);
        try {
            jsonObject.put(key, wrap(bundle.get(key)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    String message = jsonObject.get("body").toString();

    // Prepare a notification with vibration, sound and lights
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
    ...
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT));

    // Automatically configure a Notification Channel for devices running Android O+
    Pushy.setNotificationChannel(builder, context);

    // Get an instance of the NotificationManager service
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

    // Build the notification and display it
    notificationManager.notify(1, builder.build());
}

}

1 Ответ

0 голосов
/ 25 сентября 2019

Вы можете создать локальный вещательный приемник в своей деятельности

BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if(intent.getAction() == "my_action"){
                    intent.getStringExtra("data");
                }
            }
        };

        LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, new IntentFilter("my_action"));

В классе PushReceiver

Intent intent = new Intent("my_action");
intent.putExtra("data", message);
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
...