簡體   English   中英

BroadcastReceiver 未從 NotificationManager 觸發

[英]BroadcastReceiver not firing from 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();

}}

我從 FirebaseMessagingService 調用 sendNotification,通知顯示正常。

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

單擊通知或注意操作時,BroadcastReceiver onReceive 未調用,我已經在 mainafist 中注冊了我的 BroadcastReceiver

    <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>

奇怪的是我創建了一個新應用程序並將上面的所有代碼復制到它,我從 onCreate() 調用了 sendNotification,當單擊通知時它調用 onReceive 沒有問題。

我也對我的應用程序進行了相同的嘗試,並從我的主要活動的 onCreate 調用了 sendNotification,出現了通知,但單擊了通知或未調用 onReceive 的注意操作

為什么它在我的應用程序中不起作用

我必須先卸載該應用程序,然后重新安裝,現在它可以運行了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM