Daniel
Daniel

Reputation: 108

BroadcastReceiver doesn't work for the second time

I'm trying to schedule notifications with AlarmManager It works perfectly when I schedule one notification but when I schedule two notification, the first notification is okay but the second one not works.

I figured out opening the app after few minutes will notify the second notification. I think something is wrong with my BroadcastReceiver

MainActivity.java

Intent intent = new Intent(context,NotificationClass.class);
intent.putExtra("notification_id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,id,intent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);

Notification.java

public class NotificationClass extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int id = intent.getIntExtra("notification_id",0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"1")
            .setContentTitle("Notification")
            .setContentText("Content")
            .setSmallIcon(R.drawable.notif_ic);

        Notification notification = builder.build();
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("1","test", NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(id,notification);
}

AndroidManifest.xml

<receiver android:name=".NotificationClass" ></receiver> 

I don't know what is wrong with my code. Can anybody help me with this?

Upvotes: 2

Views: 1230

Answers (3)

Abdul
Abdul

Reputation: 887

I have tried it and it is working. Add your notification code inside onReceive.

Broadcast Receiver

class AlarmReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {

 /*
  Your implementation

   */
 }
}

Mainfest

    <receiver
        android:name=".AlarmReceiver"
        android:exported="true"
        android:enabled="true" />

Creating pending intents

val alarmManager = activity.getSystemService(Activity.ALARM_SERVICE) as AlarmManager
        val alarmIntent = Intent(activity.applicationContext, AlarmReceiver::class.java) // AlarmReceiver1 = broadcast receiver

        val calendar = Calendar.getInstance()
        calendar.timeInMillis = timeInMilliSeconds

        val pendingIntent = PendingIntent.getBroadcast(activity, timeInMilliSeconds.toInt(), alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT)
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)

Upvotes: 1

iamnaran
iamnaran

Reputation: 1963

Broadcast receiver to receive the data:

private BroadcastReceiver mReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String alertMessage = intent.getStringExtra("type");

        doNotificationAlertWorkHere(alertMessage);
    }
};

Register & Unregister your broadcast to avoid static leaks.

via the Android manifest file. (Statically)

<receiver android:name="YourBroadcastReceiverName"> </receiver>

via the Context.registerReceiver() and Context.unregisterReceiver() methods. (Dynamically)

 @Override
    protected void onPause() {
        super.onPause();
        // unregister broadcast
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // register broadcast
        IntentFilter filter = new IntentFilter(Constants.ACTION);
        LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
    }

Send Broadcast like:

// public static final String ACTION = "ALERT";

Intent intent = new Intent(Constants.ACTION);
intent.putExtra("type", "SUP BRO. Stay Inside");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

Knowledge Note :- Broadcast receiver is like a Cannon-fire to score a hit, you have to determine what to fire (eg. msg), where to fire (eg. activity). Load & unload the cannon to score another hit. (eg. Register & Unregister)

Upvotes: 1

Chuong Le Van
Chuong Le Van

Reputation: 284

First, make sure your notification Id is difference every single time you create a notification

Second, you miss tag intent-filter inside tag receive in manifest. pls check this https://developer.android.com/guide/components/broadcasts.

Hope this help!

Upvotes: 0

Related Questions