pushy / pushy-flutter

The official Pushy SDK for Flutter apps.
Apache License 2.0
21 stars 19 forks source link

Android Notification #53

Closed rwaidKalka closed 1 year ago

rwaidKalka commented 1 year ago

Android notification is not receiving when app is closed

pushy commented 1 year ago

Hi @rwaidKalka, Thanks for reaching out. We'd be glad to assist.

Please refer to the following article in our support center for help on this topic: How can I send notifications to Android devices in Doze / power saving mode?

rwaidKalka commented 1 year ago

Thanks for your instructions i already did that one too i disabled the battery optimization and still the notification is not reached to us while the app is terminated is there anything else i can try to solve this issue ? i am using flutter framework for the app development and the ios doesn't have any issue only android has this issue.

pushy commented 1 year ago

Hi @rwaidKalka, We'd be glad to help you investigate this issue further.

If you could please provide the following information about a device experiencing this issue, that would be great:

1) Device manufacturer 2) Device model name 3) Android OS version

rwaidKalka commented 1 year ago

Thanks for your reply actually we are facing this problem with all android devices its not specific for a model or one device.

pushy commented 1 year ago

Hi @rwaidKalka, Can you please share your project's main.dart? You may also send it to us privately at support@pushy.me.

rwaidKalka commented 1 year ago

Sure i am gonna send it right now but i did the configuration in another file which is app.dart i will send both of them from the main.dart i called tha app.dart file in the runApp.

rwaidKalka commented 1 year ago

Hello I already sent the code to the provided email but i didn't receive any email.

On Wed, Jun 7, 2023, 8:44 AM Pushy @.***> wrote:

Hi @rwaidKalka https://github.com/rwaidKalka, Can you please share your project's main.dart? You may also send it to us privately at @.***

— Reply to this email directly, view it on GitHub https://github.com/pushy/pushy-flutter/issues/53#issuecomment-1579940412, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQS3US62VVCHENXVSY77LXTXKAINFANCNFSM6AAAAAAY2E7VLQ . You are receiving this because you were mentioned.Message ID: @.***>

pushy commented 1 year ago

Hi @rwaidKalka, Thanks for sending over your code.

You're experiencing this behavior because your backgroundNotificationListener is inside app.dart instead of being inside main.dart.

As per the Flutter docs, please ensure the following method is inside your main.dart file (remove it from app.dart), right after the import statements, and outside any Widget class declaration, to process push notifications in the background via a Flutter background isolate:

// Please place this code in main.dart,
// After the import statements, and outside any Widget class (top-level)

@pragma('vm:entry-point')
void backgroundNotificationListener(Map<String, dynamic> data) {
    // Print notification payload data
    print('Received notification: $data');

    // Notification title
    String notificationTitle = 'MyApp';

    // Attempt to extract the "message" property from the payload: {"message":"Hello World!"}
    String notificationText = data['message'] ?? 'Hello World!';

    // Android: Displays a system notification
    // iOS: Displays an alert dialog
    Pushy.notify(notificationTitle, notificationText, data);

    // Clear iOS app badge number
    Pushy.clearBadge();
}
rwaidKalka commented 1 year ago

Thanks a lot we are glad to have a team like this for push notification service that was really helpful.

https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail Virus-free.www.avast.com https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

On Mon, 12 Jun 2023 at 08:41, Pushy @.***> wrote:

Hi @rwaidKalka https://github.com/rwaidKalka, Thanks for sending over your code.

You're experiencing this behavior because your backgroundNotificationListener is inside app.dart instead of being inside main.dart.

As per the Flutter docs https://pushy.me/docs/additional-platforms/flutter, please ensure the following method is inside your main.dart file (remove it from app.dart), right after the import statements, and outside any Widget class declaration, to process push notifications in the background via a Flutter background isolate:

// Please place this code in main.dart, // After the import statements, and outside any Widget class (top-level)

@pragma('vm:entry-point') void backgroundNotificationListener(Map<String, dynamic> data) { // Print notification payload data print('Received notification: $data');

// Notification title
String notificationTitle = 'MyApp';

// Attempt to extract the "message" property from the payload: {"message":"Hello World!"}
String notificationText = data['message'] ?? 'Hello World!';

// Android: Displays a system notification
// iOS: Displays an alert dialog
Pushy.notify(notificationTitle, notificationText, data);

// Clear iOS app badge number
Pushy.clearBadge();

}

— Reply to this email directly, view it on GitHub https://github.com/pushy/pushy-flutter/issues/53#issuecomment-1586618874, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQS3USZVEPSJHPGTY3ZUVYLXK2TZDANCNFSM6AAAAAAY2E7VLQ . You are receiving this because you were mentioned.Message ID: @.***>

bawahakim commented 6 months ago

For future readers, it seems I was able to setup the entry point as a static method in an arbitrary class, and background notification (app in terminated state) still works

class MyClass {
  @pragma('vm:entry-point')
  static void _backgroundNotificationListener(Map<String, dynamic> data) {
    ...
   }
  void init() {
      Pushy.listen();
      Pushy.setNotificationListener(_backgroundNotificationListener);
      ...
  }
}

Seems flutter documentation allows it, and the package awesome_notifications does it this way too https://pub.dev/documentation/flutter_isolate/latest/