Android 应用程序未运行时推送通知未发送到我的手机?

Android 应用程序未运行时推送通知未发送到我的手机?,android,firebase,push-notification,notifications,firebase-cloud-messaging,Android,Firebase,Push Notification,Notifications,Firebase Cloud Messaging,我的应用程序未运行时,推送通知无法显示。问题出在我的华为P9手机上,但在模拟器中一切正常。当应用程序正在运行,并且我已签入应用程序>MyApp>通知并启用推送通知时,将显示通知。为什么呢 我想问题是,不知何故,我需要在某个地方选中一些复选框,以便在应用程序未运行时启用通知。在emulator上一切正常的时候,看到我的手机(和朋友的手机)上发生了这种情况,我想我应该在设置活动中设置一些复选框,以便用户启用推送通知(即使应用程序未运行)?我该怎么做 更新1 public class EventNot

我的应用程序未运行时,推送通知无法显示。问题出在我的华为P9手机上,但在模拟器中一切正常。当应用程序正在运行,并且我已签入应用程序>MyApp>通知并启用推送通知时,将显示通知。为什么呢

我想问题是,不知何故,我需要在某个地方选中一些复选框,以便在应用程序未运行时启用通知。在emulator上一切正常的时候,看到我的手机(和朋友的手机)上发生了这种情况,我想我应该在设置活动中设置一些复选框,以便用户启用推送通知(即使应用程序未运行)?我该怎么做

更新1

public class EventNotificationService extends FirebaseMessagingService {
    public static final String TAG = "EventNotifyService";

    public EventNotificationService() {

    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Map<String, String> messageData = remoteMessage.getData();

        if(messageData.containsKey("notification_type")) {
            if (Integer.parseInt(messageData.get("notification_type")) == 1){
                onEventNotification(remoteMessage);
            }

            else if (Integer.parseInt(messageData.get("notification_type")) == 2) {
                onReservationMessage(remoteMessage);
            }
        }
    }

    public void onEventNotification(RemoteMessage remoteMessage) {
        RemoteMessage.Notification notification = remoteMessage.getNotification();
        Map<String, String> event = remoteMessage.getData();

        Intent intent = new Intent(this, EventActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(EventActivity.EXTRA_EVENT_URL, "http://clzola.com/api/v1/event/show/public?id=" + Integer.parseInt((String)event.get("id")));

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

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

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle(notification.getTitle())
                .setContentText((String) event.get("title"))
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }


    private void onReservationMessage(RemoteMessage remoteMessage) {

    }
}

发布您的代码,到目前为止您尝试了什么?我还没有做任何设置活动,但是对于扩展FirebaseMessagesService的my类,它可以很好地工作,就像在模拟器上一样,除了真实的设备之外,相同的应用程序工作起来没有任何缺陷,推送通知不会仅在应用程序未运行时显示@FerdousAhamedPost扩展FireBaseMessages服务的类是否正在发送
通知
数据
FCM?当您自己的应用程序未运行时,Google Play services应用程序会发布具有
通知
有效负载的FCM。为确保推送消息始终发送到您的
FirebaseMessagingService
,切勿发送
通知
有效负载。将构建通知所需的所有内容放入
数据
有效负载中。
$requestBody = [
        "to" => "/topics/event_category_" . $event->event_category_id,
        "notification" => [
            "body" => $event->title,
            "title" => "New event created",
            "sound" => "default",
            "priority" => "high",
        ],
        "data" => [
            "notification_type" => 1,
            "id" => $event->id,
            "title" => $event->title,
        ]
    ];

    $client = new \GuzzleHttp\Client();
    $response = $client->post('https://fcm.googleapis.com/fcm/send', [
        "headers" => [
            "Content-Type" => "application/json",
            "Authorization" => "key=" . env("FCM_SERVER_KEY"),
        ],
        "body" => json_encode($requestBody)
    ]);