为什么我的 android 通知不会发送?

Why will my android notification not send?

我正在从 Postman 发送 DATA 负载,因此无论应用程序是在前台还是后台,都应该在 onMessageReceived 中处理通知。

根据我的日志和测试,触发了 onMessageReceived,但未显示通知(不取决于应用程序是在前台还是后台)。

清单中我有:

<service
        android:name=".MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service> <!-- [END firebase_iid_service] -->
    <service
        android:name=".MyFirebaseInstanceIDService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>

这里是 MyFirebaseMessagingService.java:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        String title = "";
        if (Objects.requireNonNull(remoteMessage.getData()).get("title") != null)
            title = remoteMessage.getData().get("title");

        String message = "";
        if (Objects.requireNonNull(remoteMessage.getData()).get("message") != null)
            message = remoteMessage.getData().get("message");

        Log.e("notification",title);
        sendNotification(title, message);
    }

    private void sendNotification(String title, String body) {

        Intent i = new Intent(this, Novinky.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pi = PendingIntent.getActivity(this,
                0, // Request code
                i,
                PendingIntent.FLAG_ONE_SHOT);

        Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
            getString(R.string.default_notification_channel_id))
                .setSmallIcon(R.mipmap.ic_notify)
                .setContentTitle(title)
                .setContentText(body)
                .setAutoCancel(true)
                .setSound(sound)
                .setContentIntent(pi);

        NotificationManager manager =
                (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        assert manager != null;
        manager.notify(0, builder.build());
    }
}

所以我有 Log.e("notification",title);在那里,它执行正常,但没有收到通知。

在我还通过 Postman 发送通知+数据之前,这是有效的,但这只是一个自动通知,应用程序不处理,所以现在我只想发送数据负载并在应用程序中处理它,但对于某些它不起作用的原因。

您需要确保已在您的应用中创建通知渠道。一个好的方法如下:

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        createNotificationChannel();
    }

    private void createNotificationChannel() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel nChan = new NotificationChannel(
                getString(R.string.default_notification_channel_id),
                "My_Channel",
                NotificationManager.IMPORTANCE_DEFAULT
            );

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(nChan);
        }
    }
}

在应用程序的 onCreate() 中创建通知渠道是个好主意,因为您不必在应用程序的剩余生命周期内担心它。