1

I got a problem with notification in the broadcast receiver.

Here's Class:

public class JokeAlarmReceiver extends BroadcastReceiver {

CompositeDisposable compositeDisposable = new CompositeDisposable();

public static final String NOTIFICATION_CHANNEL_ID = "Channel Joke ID";
public static final String NOTIFICATION_CHANNEL_NAME = "Channel";
public static final int NOTIFICATION_ID = 123;

@Inject
DataManager dataManager;


@Override
public void onReceive(Context context, Intent intent) {
    ((JokeApplication)context.getApplicationContext()).getAppComponent()
            .plus(new JokeAlarmReceiverModule(context))
            .inject(this);


    getJokesToNotification();
    createNotification(context);

}

public void createNotification(Context context) {

        final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_mood_black_24dp)
                .setContentTitle("Smile Reminder :)")
                .setContentText(dataManager.getJokeForNotification())
                .setStyle(new NotificationCompat.BigTextStyle())
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setAutoCancel(true);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    NOTIFICATION_CHANNEL_ID,
                    NOTIFICATION_CHANNEL_NAME,
                    NotificationManager.IMPORTANCE_DEFAULT);

            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(NOTIFICATION_ID, mBuilder.build());


    }



public void getJokesToNotification(){

    compositeDisposable.add(dataManager.getJokes()
           .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    randomJokes -> {
                        // onNext
                            dataManager.setJokeForNotification(randomJokes.getSetup(),randomJokes.getPunchline());
                        Log.d("WORKING","1st");
                        },
                    throwable -> {
                        // onError
                    },
                    () -> {
                        // onCompleted
                    })
    );
}

In short, how it works... I used the method with rxjava to make a call for API what should give me object RandomJoke from which I used 2 parts with text. I Inject dataManager to which I store text. Then I use this text as a text for notification. I wanna make notification appear repeat even when my app is closed. The problem is all working fine when the app is still working but when I close it... fist Notification I get got null instead text and second and rest notification working fine. Can anyone tell what am doing wrong? The broadcast is registered via manifest and I test everything on Emulator with API 25

Arslanbekov Denis
  • 1,674
  • 12
  • 26
Kamil.W
  • 11
  • 1
  • 5

0 Answers0