rafaelsetragni / awesome_notifications

A complete solution to create Local Notifications and Push Notifications, throught Firebase or another services, using Flutter.
https://discord.awesome-notifications.carda.me
Apache License 2.0
749 stars 324 forks source link

Notification not received on IOS after first click on it #601

Closed Niko-by closed 1 year ago

Niko-by commented 1 year ago

1 - Describe the bug

Hello!

I send a notification to iOS, it arrives successfully, I click on it and the application opens. Then I close the app with a swipe up and try to send a new notification, but it doesn't get delivered. Then I hit submit again (third notification) and it's successfully received.

That is, after clicking on a notification and closing the application, the next notification will not be delivered.

Tell me what could be the problem?

2 - Provide us as much information as possible

3 - How To Reproduce the Error

  1. Close the application with a swipe up.
  2. Send a notification.
  3. Click on the notification.
  4. Close the application with a swipe up.
  5. Send a notification (it won't be delivered)
  6. Send another notification (now successfully delivered)

4 - Expected behavior

I expect that after clicking on a notification and closing the app, the next sent notification will also be delivered.

5 - Screenshots

6 - Additional context

awesome_notifications: ^0.7.2 firebase_messaging: ^13.1.0

rafaelsetragni commented 1 year ago

If your app is closed with terminated (forced to stop) status, on any platform, any push until the app is reopened by the user will be denied. The concept of forced to stop may change between Android distributions.

Also, you shouldn't use awesome_notifications and firebase_messaging together. Take a look at: https://github.com/rafaelsetragni/awesome_notifications#%EF%B8%8F-attention-%EF%B8%8F--users-from-firebase_messaging-plugin

Niko-by commented 1 year ago

If your app is closed with terminated (forced to stop) status, on any platform, any push until the app is reopened by the user will be denied. The concept of forced to stop may change between Android distributions.

Maybe I explained badly

  1. We use iOS, not Android
  2. Notifications are delivered via Firebase. We are using a push token from Firebase. The notification is sent from the server side, DATA section, and through Awesome they are displayed. AwesomeNotifications().createNotificationFromJsonData
  3. Earlier I described how to reproduce. After closing the application, the first notification is not delivered. Everything else is delivered. This only happens when you clicked on the notification before closing the app. BUT, if you open the app directly and close it with a swipe up, the notifications are successfully delivered! This is very strange behaviour.

How to reproduce the error:

rafaelsetragni commented 1 year ago

Try to get your device log using "Console.app" on macOS and you can find out why your notification is not being delivered.

image

OBS: awesome_notifications_fcm also uses Firebase services. Are you using awesome_notifications_fcm in your project?

Niko-by commented 1 year ago

On first submit in macos console I see this error:

AMSURLSession: [AEE04261] Task completed with error = Error Domain=AMSErrorDomain Code=301 "Invalid Status Code" UserInfo={NSLocalizedFailureReason=The response has an invalid status code, AMSURL=https://sandbox.itunes.apple.com/commerce /messages/app/inbox?bundleId=com.realconstruction.ios&guid=...........&status=1, AMSStatusCode=412, NSLocalizedDescription=Invalid Status Code}

When resending, there is no error, the notification was delivered (at the same time, the application did not start, it was still closed, I think this is not related to closing the application).

Niko-by commented 1 year ago

But I have an old code implementation, I still use awesome with firebase_messaging.

I will try to transfer the project to awesome_notifications_fcm as you suggested, and later I will write back whether it helped or not.

rafaelsetragni commented 1 year ago

AMSURLSession: [AEE04261] Task completed with error = Error Domain=AMSErrorDomain Code=301 "Invalid Status Code" UserInfo={NSLocalizedFailureReason=The response has an invalid status code, AMSURL=https://sandbox.itunes.apple.com/commerce /messages/app/inbox?bundleId=com.realconstruction.ios&guid=...........&status=1, AMSStatusCode=412, NSLocalizedDescription=Invalid Status Code}

Take a look at this: https://developer.apple.com/forums/thread/661351?page=3

Niko-by commented 1 year ago

Take a look at this: https://developer.apple.com/forums/thread/661351?page=3

It turned out that this error does not apply to us. It disappeared when I switched from TestFlight to the version from the Appstore.

Therefore, the question remains open, I do not understand why the first notification does not come after the application is closed. But all subsequent notifications successfully reach, although the application still remains completely closed. I didn't notice any other errors in the console.

Can you test this behavior for yourself? The notification is sent from the server, here is the configuration:

{ data: { content: JSON.stringify({ id: id, channelKey: 'real_construction_channel_key', title: title, body: desc, icon: 'resource://drawable/push', color: '#0774EA', showWhen: true, payload: payload ? payload : {}, }), }, android: { priority: "high", }, apns: { payload: { aps: { contentAvailable: true, }, }, headers: { "apns-push-type": "background", // Must be 5 when contentAvailable is set to true. "apns-priority": "5", "apns-topic": "com.realconstruction.ios", }, }, tokens: tokens, content_available: true, priority: "high", }

rafaelsetragni commented 1 year ago
"apns": {
    "payload": {
        "aps": {
            "contentAvailable": true,
        },
    },
    "headers": {
        "apns-push-type": "background",
        // Must be 5 when contentAvailable is set to true.
        "apns-priority": "5",
        "apns-topic": "com.realconstruction.ios",
    },
},

This can be problematic. as you're using your push as a silent mode with background and contentAvailable. This makes your push be handle as background processing and suffer for some execution restrictions. You gonna do this instead:

{
    tokens: tokens,
    mutable_content: true,
    priority: "high",
    notification: {
        title: title,
        body: desc,
    },
    data: {
        content: JSON.stringify({
            id: id,
            channelKey: 'real_construction_channel_key',
            title: title,
            body: desc,
            icon: 'resource://drawable/push',
            color: '#0774EA',
            showWhen: true,
            payload: payload ? payload : {},
        }),
    },
}

That's why i created the FCM addon plugin, this way you can use normal push notifications and they are more reliable than silent ones.

Niko-by commented 1 year ago

But if we add a block

notification: {
    title: title,
    body:desc
}

then the app will show 2 notifications. The first is from firebase and the second is from awesome. This behavior has been seen on Android. Because I am processing the content through createNotificationFromJsonData. At least that's how it used to be. That's why I use silent notification.

In addition, quick buttons (actionButtons) will not work on ios if you send a notification with a block

notification: {
    title: title,
    body:desc
}
rafaelsetragni commented 1 year ago

But if we add a block notification: { title: title, body:desc } then the app will show 2 notifications. The first is from firebase and the second is from awesome. This behavior has been seen on Android. Because I am processing the content through createNotificationFromJsonData. At least that's how it used to be. That's why I use silent notification.

This is a sign that youre using firebase_messaging, right?

Niko-by commented 1 year ago

This is a sign that youre using firebase_messaging, right?

Yes. So far, we have the old implementation in the code. Is this fixed in awesome_notifications_fcm?

rafaelsetragni commented 1 year ago

Yes, now, thanks to the FCM plugin, this type of push notification is fully supported. Also make sure you use the latest version of all three plugins and follow these steps to properly configure your project.

🛠 Getting Started https://github.com/rafaelsetragni/awesome_notifications_fcm#-getting-started

First, as a good practice, I recommend that you create a backup copy or switch to a different branch to make such big changes to your project.

Niko-by commented 1 year ago

Ok. I will try and tell you later the result

github-actions[bot] commented 1 year ago

This issue was automatically closed due inactivity, but can be reopened at any time.