简体   繁体   中英

BroadcastReceiver not firing from NotificationManager

I have the following code:

 public void sendNotification() {
    try {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
        final Intent intent = new Intent(this, NotifyBroadcastReceiver.class);

        //Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.journaldev.com/"));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo));
        builder.setContentTitle("Notifications Title");
        builder.setContentText("Your notification content here.");
        builder.setSubText("Tap to view the website.");
        builder.setAutoCancel(true);
        final Intent noted = new Intent(this, NotifyBroadcastReceiver.class);
        noted.setAction("com.mawaeed.common.LaunchActivity");
        PendingIntent notedpendingIntent = PendingIntent.getBroadcast(this, 0, noted, 0);//  PendingIntent.FLAG_UPDATE_CURRENT ) ;
        builder.addAction(0, "Noted", notedpendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Will display the notification in the notification bar
        notificationManager.notify(1, builder.build());
    }catch(Exception exo) {
        Toast.makeText(this, exo.toString(),Toast.LENGTH_LONG).show();

    }
}

Also I have my BroadcastReceiver

public class NotifyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context,"ddd",Toast.LENGTH_LONG).show();
    Toast.makeText(context,"app",Toast.LENGTH_LONG).show();

}}

I am calling sendNotification from FirebaseMessagingService, Notification appears normally.

   public void onMessageReceived(RemoteMessage remoteMessage) {
        sendNotification();
}

When clicking on the notification or Noted action, BroadcastReceiver onReceive not calling, I already registered my BroadcastReceiver in mainafist

    <receiver android:name=".NotifyBroadcastReceiver"  android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
        </intent-filter>
    </receiver>

The strange thing is I created a new application and copied all the code above to it, and I called the sendNotification from onCreate(), and when clicking on the notification it calls onReceive without problem.

I also tried same with my Application and called sendNotification from onCreate of my main activity, Notification appears but clicking on notification or Noted action not calling onReceive

Why it is not working from my application

I had to uninstall the app first then install it again, and now it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM