当应用未运行时,推送通知未发送到我的手机?

时间:2017-05-13 19:36:48

标签: android firebase push-notification notifications firebase-cloud-messaging

我的应用程序运行时没有显示推送通知的问题。问题出在我的华为P9手机上,但在模拟器中一切正常。应用程序运行时我会检查应用程序> MyApp>启用通知和推送通知。那是为什么?

我想问题是,我不得不在某个地方检查某个方框以在应用未运行时启用通知。在我的手机(以及我的朋友)上看到这种情况发生在模拟器上的一切正常我觉得我的设置活动中应该有一些复选框,用户可以启用推送通知(即使应用程序没有运行)?我该怎么做?

更新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) {

    }
}

用于从我的服务器发送通知的代码:

$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)
    ]);

0 个答案:

没有答案