BroadcastReceiver не срабатывает из NotificationManager

У меня есть следующий код:

 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();

    }
}

Также у меня есть 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();

}}

Я звоню sendNotification из FirebaseMessagingService, уведомление появляется нормально.

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

При нажатии на уведомление или отмеченное действие BroadcastReceiver onReceive не вызывается, я уже зарегистрировал свой BroadcastReceiver в 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>

Странно то, что я создал новое приложение и скопировал в него весь код выше, и я вызвал sendNotification из onCreate(), и при нажатии на уведомление он вызывает onReceive без проблем.

Я также пробовал то же самое с моим приложением и вызывал sendNotification из onCreate моей основной деятельности, появляется уведомление, но нажатие на уведомление или отмеченное действие не вызывает onReceive

Почему это не работает из моего приложения


person Thudner    schedule 24.04.2020    source источник


Ответы (1)


Мне пришлось сначала удалить приложение, а затем установить его снова, и теперь оно работает.

person Thudner    schedule 25.04.2020