kreait / laravel-firebase

A Laravel package for the Firebase PHP Admin SDK
https://github.com/kreait/firebase-php
MIT License
995 stars 161 forks source link

firebase message not being displayed as heads up banner #33

Closed nicolasvahidzein closed 4 years ago

nicolasvahidzein commented 4 years ago

Hello, i am able to send a message via laravel to my app but when in the bg the notification is the default silent notification. My notification is on high but nothing changes.


        $deviceToken = 'fSQJb3T1tsg:APA91bFN80J7kJSd0S-lSHfpk8pHCo7EQc38rA7ymK4';

        //FIX_THIS_SOON
        //remove this later

        $notificationTitle = 'Package is being delivered';
        // $notificationBody = 'Your order 9847GH8409 is on it\'s way to you.'; //version 1
        // $notificationBody = 'Your order 9847GH8419 is on it\'s way to you.'; //version 2
        $notificationBody = 'Your order 9847GH8428 is on its way to you.'; //version 3
        $notificationIcon = 'http://lorempixel.com/400/200/'; //
        $notificationImageUrl = 'http://lorempixel.com/400/200/';
        $notificationPriority = 'high';

        /* $notification = Notification::fromArray([
            'title' => $notificationTitle,
            'body' => $notificationBody,
            // 'image' => $imageUrl,
        ]); */

        // $notification = Notification::create($notificationTitle, $notificationBody);

        // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#notification
        $notificationPayload = [

            'title' => $notificationTitle,
            'body' => $notificationBody,
            //'image' => 'http://lorempixel.com/400/200/',
        ];

        $data = [
            'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
            'action' => 'new_notification',
            'notification_id' => '888888888',
            'notification_uid' => '1111-2222-3333-4444',
            'notification_foreignUID' => '5555-6666-7777-8888',
            'notification_foreignUID_type' => 'user',
            'notification_title' => $notificationTitle,
            'notification_message' => $notificationBody,
            'notification_asset_type' => 'image',
            'notification_image' => 'http://127cf52a.ngrok.io/storage/images_products/860a6ac4-bd66-4c3b-b07f-450f7cdcc007_2020-01-20_15-15-21_0001.jpg',
            'notification_icon' => null,
            'notification_account_type' => 'client',
            'notification_read_status' => '0',
            'notification_status' => 'active',
            'notification_created_at' => '2020-03-29 02:09:00',
            'notification_updated_at' => '2020-03-29 02:09:00',
        ];

        // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#androidconfig
        $androidSettings = [
            'ttl' => '3600s',
            'priority' => $notificationPriority,
            'notification' => [
                'title' => $notificationTitle,
                'body' => $notificationBody,
                // 'icon' => 'stock_ticker_update', //optional
                // 'color' => '#f45342', //optional
            ],
        ];

        // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#apnsconfig
        $apnsSettings = [
            'headers' => [
                'apns-priority' => '10',
            ],
            'payload' => [
                'aps' => [
                    'alert' => [
                        'title' => $notificationTitle,
                        'body' => $notificationBody,
                    ],
                    'badge' => 42,
                ],
            ],
        ];

        // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#webpushconfig
        $webpushSettings = [
            'notification' => [
                'title' => $notificationTitle,
                'body' => $notificationBody,
                // 'icon' => $notificationIcon, //optional
            ],
        ];

        //hide fcm_options if you are leaving it empty
        // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#fcmoptions
        $fcmOptions = [
            'analytics_label' => 'some-analytics-label' //optional
        ];

        $message = new RawMessageFromArray([
            'token' => $deviceToken,
            'notification' => $notificationPayload,
            'data' => $data,
            'android' => $androidSettings,
            'apns' => $apnsSettings,
            'webpush' => $webpushSettings,
            'fcm_options' => $fcmOptions
        ]);

        $messaging = app('firebase.messaging');

        $messaging->send($message);
jeromegamez commented 4 years ago

There are many factors in play that influence if a message on the client is actually displayed as a notification or not - while I don't immediately see an issue in the fields you're using (and the Firebase API is accepting it, so it's fine by itself), I also can't tell you what might cause the unwanted behavior.

My advice would be to try with the simplest possible message at first and then add the additional fields until it doesn't work as expected anymore.

If you're using an array, you pass it directly to Messaging::send() (except, of course, there's a reason you're using RawMessageFromArray instead of CloudMessage)

$messaging->send([
    'token' => $deviceToken,
    'notification' => [
        'title' => 'My title',
        'body' => 'My body',
    ],
]);

One of the factors that might come into play: I'm testing FCM messages with Firefox on my computer. I receive messages as notification only if I'm on another tab - just having the browser minimized or hidden is not enough. I don't know what affects notifications on Android systems and when a message is delivered directly into the system tray or when an actual visual notification is triggered.

All in all, there's not much I can do from an SDK perspective, so I'm closing this issue - but feel free to continue the conversation or add your findings once you've figured it out to help others that might stumble upon this! 🙏

nicolasvahidzein commented 4 years ago

I guess i mispoke. Its working perfectly fine except its a silent notification. I want the heads up style. People say you need high priroty then they say you need sound. Its a mess. The they say omit the notification part. I was wondering if you own an android if you can send a sample message from kreit that confirm comes in as a heads up notification and not a silent notification. Otherwise i get them and your package is awesome. I was never faulting you.

jeromegamez commented 4 years ago

Oh, I didn‘t think you did, but thank you so much for clearing this up anyway 🌺.

I unfortunately don‘t own an android device (and don‘t have an own application I could test this with), I only ever test with browser notifications 🙈.

What I do know is that a message definitely needs a notification with a title and a body to be displayed as a popular notification (I believe that’s what you mean with „heads up“ notification).

As I don‘t know for sure either, I can only ask questions as some kind of a „check list“ to clear possible issues - if you have already checked it, just ignore :)

I haven‘t more ideas at the moment, but if something comes to mind, I‘ll add it 🤞

nicolasvahidzein commented 4 years ago

Everything is tip top. All the answers to your questions is positive.

Not silent Enabled They arrive perfectly in all 3 situations running, bg, resume Simple works but shows the same as silent notification Yes reinstalled Yes correct token. It fails if token is old or unregistered or fake

jeromegamez commented 4 years ago

If you compose and send a notification from the Firebase Console (in the Browser), does it work as you would expect it?

nicolasvahidzein commented 4 years ago

that's my problem, i don't have a baseline as FCM console removed the priority option. People say to send the notification with priority in data portion but it does nothing. Where do we set priority in fcm in your sdk? can it be somewhere else than in the android section of the raw message?

nicolasvahidzein commented 4 years ago

also, how do i set thisin Kreit?

   "webpush": {
      "headers": {
        "Urgency": "high"
      }
    }
nicolasvahidzein commented 4 years ago

Also i found this: https://developer.android.com/training/notify-user/build-notification#Priority

private fun createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = getString(R.string.channel_name)
        val descriptionText = getString(R.string.channel_description)
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
            description = descriptionText
        }
        // Register the channel with the system
        val notificationManager: NotificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}

How do i set this in kreit?

Thanks.

jeromegamez commented 4 years ago

I don‘t know what the Android is all about, but from the backend we‘re limited/enabled to what the REST API supports, you can find the specification here:

https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages

If you find that the CloudMessage doesn‘t support one of these, please let me know. However you should be able to use the full specification if you pass it to the RawMessageFromArray class.

nicolasvahidzein commented 4 years ago

Ok will revert back. For enum types what do i type since it cannot detect the associated classes?

jeromegamez commented 4 years ago

What do you mean by „revert“?

You can continue to use CloudMessage or RawMessageFromArray, but the message payload must match the specifications of the REST API.

As for the enums, you can use their values, I don‘t know how enums work with Java, but in the case of e.g. NotificationManager.IMPORTANCE_DEFAULT, use either the string 'IMPORTANCE_DEFAULT', the value you find behind that enum or use the correspondent value in the docs.

nicolasvahidzein commented 4 years ago

I can't explain this, i am not using the most complete api rest call in the history of humanity and still nothing.

https://stackoverflow.com/questions/61166309/android-heads-up-firebase-notification-not-working

I have no idea why android is so backwards like this. It should be extremely simple but no dice.

fakingfantastic commented 4 months ago

I can't explain this, i am not using the most complete api rest call in the history of humanity and still nothing.

https://stackoverflow.com/questions/61166309/android-heads-up-firebase-notification-not-working

I have no idea why android is so backwards like this. It should be extremely simple but no dice.

Hey @nicolasvahidzein , on your SO post you left a comment saying you found the solution. Can you please post it here so others can benefit.

Thanks,

nicolasvahidzein commented 4 months ago

Ping me on skype please nzein19. Ill show you and you can paste it here to help everyone. My time is too limited.

fakingfantastic commented 4 months ago

Hey everyone,

I struggled with this issue for a long time - turns out the issue isn't truly related to kreait/laravel-firebase.

The issue is documented pretty well here: https://developer.android.com/develop/ui/views/notifications#ManageChannels

TL;DR - newer versions of Android require you to declare a notification channel to use if you want to use heads-up notifications, as the default channel that gets set up isn't capable of things like heads-up notification, or vibrate, or flashing light.

Set up a channel in MainApplication.java, like the following comment outlines: https://github.com/invertase/react-native-firebase/issues/2791#issuecomment-552616427

From there, just make sure to push your Notification to that channel on Android:

   new FcmMessage(
            notification: [
              'title' => $title
            ],
            custom: [
                'android' => [
                    'notification' => [
                        'title' => $title,
                        'channel_id' => 'YOUR_CHANNEL_NAME',
                    ]
                ],

Thanks to @nicolasvahidzein to pairing with me on this

Hope it helps

jeromegamez commented 4 months ago

Thanks to both of you enabling others who might stumble upon this, four years (!) later! 🙏🏻

nicolasvahidzein commented 4 months ago

This is what makes this community amazing.