Closed fidan-mkdir closed 2 years ago
Unfortunately, the fact that it used to be working in the past and then stopped working, suggests that the problem is on the Firebase server. The ambiguous message Internal error occurred
is the message that the server sends back, so there is not really something that can be done to make it less ambiguous 😅.
As for your code - it seems to be catered to the laravel-notification-channels/fcm
so I can't assist you with it, but again, if it used to work, that's probably not the problem, but if you want me to have a look at it, please provide me with the payload just before it is sent to the Firebase servers (you could, for example, temporarily put an echo (string) $request->getBody()
into https://github.com/kreait/firebase-php/blob/94cc2699471896ec36ff9abe86af4263d10c45c6/src/Firebase/Messaging/ApiClient.php#L39-L46
Perhaps I can then see something in the payload if it stands out to me. Hopefully, it's really just a problem on the server side and it will correct itself in time 🤞
I was noticing the same problem when running the integration tests of the SDK with a test registration token that I had generated by the end of October (I'm using https://github.com/firebase/quickstart-js/tree/master/messaging to manually generate an FCM registration token and inject it into the tests via an environment variable).
Once I generated a new FCM token, the tests worked again. Are you by any chance using a pre-generated test FCM token as well?
I'm not sure why Firebase (the server) is returning the Internal Server Error
here, but it seems as if it has something to do with too old FCM device tokens. Could you try again with another, freshly created FCM token?
We generate a token when the user accesses the mobile version of the app, as it is primarily a web app, with some of the functionality now enabled through a mobile app.
The token is updated in the web app database every time a user logs on the mobile app and is deleted when they log out from the mobile app. However, this doesn't mean that it is a fresh token every time the user logs in the mobile app, but there is a handler that ensures that the token is updated in the mobile app when it is refreshed. Is this bad practice? Should we refresh the token every time the user uses the app? Occasionally, we get a not found error (because of an invalid FCM token) and that is the part in which we remove the FCM token from the user until they log in again.
The interesting part is that this issue occurred only once, so I can't exactly give you a payload of a failed message. Would a regular payload be able to shed some light?
Dump:
{
"message":{
"name":"Shift Offer",
"data":{
"shift_offer_id":"some_id_here",
"shift_request_id":"other_id_here",
"type":"shift_offer"
},
"notification":{
"title":"Shift Offer",
"body":"A shift is available at Location, Room on Date Time.",
"image":null
},
"android":{
"collapse_key":null,
"priority":null,
"ttl":null,
"restricted_package_name":null,
"data":null,
"notification":null,
"fcm_options":{
"analytics_label":"analytics_android"
}
},
"webpush":null,
"apns":{
"headers":{
"push-type":"background"
},
"payload":{
"aps":{
"content-available":1
}
},
"fcm_options":{
"analytics_label":"analytics_ios",
"image":null
}
},
"fcm_options":null,
"token":"token_printed_here",
"topic":null,
"condition":null
},
"validate_only":false
}
Thank you for providing further details, mainly that the problem has occurred only once so far. Please don't think that I want to make it too easy on myself, but I believe that this "Internal Server error" could/should be something that you treat as if the FCM token had not been found, and let your web/mobile app send register a new one and send it back to your PHP application.
try {
// send the message
} catch (\Kreait\Firebase\Exception\Messaging\NotFound $exception) {
// The token was not found = it has been unregistered on the Firebase servers
// Let the client/mobile app send us a new one
} catch (\Kreait\Firebase\Exception\Messaging\ServerError $exception) {
// The Firebase Servers responded with a 500 error
// Why? Only Firebase knows, but it happens from time to time. This can usually
// be solved by letting the client request a new registration (same behavior as NotFound)
} catch (MessagingException $exception) {
// Something else went wrong
}
I'm sorry I can't provide a more satisfying answer - I didn't dare let the SDK automatically treat 500 errors as if they were 404 errors, even though it seems to be that way. I haven't found out yet, when those errors are triggered, only that I encounter them from time to time myself, and regenerating the FCM token helps each time.
Since we're both encountering them with a web application involved, I'd guess that it might have something to do with browser updates, or changed extensions, or even notification settings. Still, it's just me speculating here.
Okay, sounds like it is best to just treat this as an invalid FCM token case and remove it from the user, then the mobile app will automatically replace it with a new one.
Thank you very much for looking into this, it is much appreciated.
You're welcome! 🌺 I'm going to close the issue because (at the moment) it doesn't seem as if this can be handled from within the SDK, but you can of course add comments in case your investigation revealed something new! Thanks!
In my case, this occurred whenever the Ionic 7 Capacitor 5 AngularJS app I had open was closed when the push notification was issued.
If the app is running - no error, if the app is closed, Internal Error encountered
error.
Here is my Laravel notification code:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Notification;
use NotificationChannels\Fcm\FcmChannel;
use NotificationChannels\Fcm\FcmMessage;
use NotificationChannels\Fcm\Resources\Notification as FcmNotification;
class RequestAccess extends Notification
{
public $url_string;
public $asset_id;
public $first_name;
public $last_name;
public $email;
public $company;
public function __construct($url_string, etc...)
{
$this->url_string = $url_string;
... // etc
}
public function via($notifiable)
{
return [FcmChannel::class];
}
public function toFcm($notifiable)
{
return (new FcmMessage())
->setData([
'url_string' => $this->url_string ?? '',
... // etc.
])->setNotification(
FcmNotification::create()
->setTitle('Access Request')
->setBody('You have a new access request')
);
}
}
Obviously I need the push notifications to fire even when the app is closed, any clues on this are appreciated, thank you.
Your code indicates that you are using the Laravel FCM notification channel package, which I don't maintain.
The library uses version 5.x of the SDK which has reached EOL and which I therefore don't support anymore (the current version is 7.x).
For these two reasons I can't provide you with in-depth support.
However, the Internal Server Error is returned by the Firebase Servers. I don't know how this relates to your app being opened or closed, but this is handling by Firebase, and can't be fixed from within the SDK.
Thanks for the courtesy response @jeromegamez - apologies for re-opening a closed thread that doesn't particularly pertain to your library. For the record, in order to hopefully help someone else encountering something similar, I upgraded my project to PHP 8.2 and upgraded my laravel-notification-channels/fcm
library to ^4.0
which released last week. It appears it still uses the 5.x SDK, but the error I was encountering (app being closed when a push is received) no longer resulted in the error I had before, so something was rectified in the midst of all the assorted PHP jumps & the channel package bump.
So thanks for the nudge in the upgrade direction. :)
I'm glad I could help at least a little, even if just indirectly 😅, and thank you for sharing how you got it sorted out, I'm sure it will help someone!
Describe the issue you are experiencing
We've been using this package to successfully send push notifications to Android and iOS devices for a while now but just yesterday an exception was caught by our Sentry. This is what it said:
As you can see, that is not much to go on. I've looked at a similar issue reported here
355
But I'm not using the topic field at all.
Here is the way I create the notification, note that I have extended the FcmChannel class to account for some custom error handling when there is a NotFound exception, unrelated to this problem anyway.
Has anyone had a similar issue? I'm really using FCM for simple text messages, so there is nothing additional.
Installed packages
PHP version and extension
7.3
On which operating system(s) does the issue occur?
Steps to reproduce the issue.
Honestly, I do not know.
Error message/Stack trace
Additional information
No response