pushy / pushy-demo-android

A demo of the Pushy Android SDK integrated into a sample Android app.
Apache License 2.0
21 stars 9 forks source link

Notification can't received when kill application #5

Closed NinhvanLuyen closed 5 years ago

NinhvanLuyen commented 5 years ago

When I kill application from multitask screen. I can't receive notification.

pushy commented 5 years ago

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

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

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

Some Chinese Android manufacturers make use of custom power-saving restrictions that inhibit sending push notifications to your users while your app is in the background. These power-saving restrictions also apply to Firebase Cloud Messaging.

Most notably, Chinese market devices from the following manufacturers include custom power-saving restrictions:

In order to receive notifications in the background on these devices, you will need to ask your users to whitelist your app from the respective manufacturer's power saving mechanism, within the device settings. The steps for achieving this are different for each manufacturer.

Grandolf49 commented 2 years ago

I'm facing the same issue on OnePlus 6 Android 11. I whitelisted my app but still don't receive notifications when app is removed from multitask screen.

Any help would be appreciated @pushy-me

pushy commented 2 years ago

Hi @Grandolf49, We'd be glad to assist.

If the affected devices are not within mainland China, we recommend implementing FCM high priority fallback delivery. This is proven to work around several of these manufacturers' custom power saving mechanisms (Xiaomi, OnePlus, and more), by having Pushy deliver your notifications through MQTT and Firebase Cloud Messaging's high-priority channel simultaneously, while only waking up your app once.

Please integrate this functionality into your app, or follow these instructions to clone and run the Pushy Demo app from source (which includes support for FCM Fallback Delivery), and confirm that the issue is resolved (the Google Play version of Pushy Demo does not include FCM Fallback Delivery as of today).

Grandolf49 commented 2 years ago

Hi @pushy-me Thanks for reverting back.

The reason why I am moving from FCM is that in my recent experience, several high-priority messages were not delivered to target devices. So in scenarios where FCM doesn't deliver Push Notifications, I'm hoping Pushy would do the job.

I integrated FCM with Pushy and it works when the app is in the background but I'm looking for a solution where Pushy can deliver PNs without FCM when the app is in the background, so that it covers the scenarios where FCM fails.

I have also gone through Pushy's FAQs about the issues when app is in BG and also disabled the Battery Optimization for my app, still facing the same issue.

Grandolf49 commented 2 years ago

I'm also open to run as Foreground Service. Can you please help on setting it up as I didn't find the documentation for it?

pushy commented 2 years ago

Hi @Grandolf49, The first recommendation is to go over the DontKillMyApp whitelisting instructions page for OnePlus:

  1. Lock the app (instructions in the page linked above)
  2. Turn off System Settings > Apps > Gear Icon > Special Access > Battery Optimization
  3. Disable App Auto-Launch for your app
  4. OnePlus 6 and up: Disable "Deep optimization or Adaptive Battery" and disable "Sleep standby optimization"
  5. OnePlus below 6: Turn off System settings > Battery > Battery optimization > (three dots) > Enhanced optimization
  6. Set "Recent app management" to "Normal clear"

After confirming the above instructions don't resolve the issue, and if your users are fine with seeing an ongoing notification in the status bar from your app, you can enable the foreground service functionality as follows:

Import version 1.0.77 of the SDK as a dependency in your app's build.gradle:

implementation 'me.pushy:sdk:1.0.77'

Add the following permission to your app's AndroidManifest.xml:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

Add the following code before Pushy.listen() in your main activity and customize the notification as desired:

// Pushy foreground service implementation
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

// Get app name as string
String appName = getPackageManager().getApplicationLabel(getApplicationInfo()).toString();
String description = "Listening for notifications";

// Android O and newer requires notification channels
// to be created prior to dispatching a notification
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel("pushy_ongoing", appName, NotificationManager.IMPORTANCE_MIN);
    channel.setDescription(description);

    // Register the channel with the system
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
}

// Create foreground notification using pushy_ongoing notification channel (customize as necessary)
Notification notification = new NotificationCompat.Builder(this, "pushy_ongoing")
                .setContentTitle(appName)
                .setContentText(description)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentIntent(pendingIntent)
                .build();

// Configure Pushy SDK to start a foreground service with this notification
// Must be called before Pushy.listen();
PushySocketService.setForegroundNotification(notification);

Run your app and observe to see if a foreground notification for the service is displayed, and whether it resolves the issue you're facing.

klausjakobsen commented 2 years ago

@Grandolf49 What did you end up doing? We're facing the same issue. Did you implement the FCM fallback integration?

Grandolf49 commented 2 years ago

@klausjakobsen Yes, we ended up implemented FCM fallback

SatishKumarRaizada commented 2 years ago

@pushy-me @Grandolf49 I tried with FCM high priority fallback delivery but do not receive Notification when app is killed. I followed the instruction and validated the

D/Pushy: FCM device token updated successfully

D/Pushy: Received push via FCM for package {...}

But app killed notification does not work.

I am using Flutter framework, tried multiple device manufacturer like Samsung, Oppo

pushy commented 2 years ago

Hi @SatishKumarRaizada, Indeed this is caused by third-party manufacturer custom power saving optimizations on the following devices:

These aggressive battery optimizations kill your app's process entirely and prevent it from starting, affecting FCM fallback delivery in some cases.

Please follow the instructions on DontKillMyApp.com for your respective device and OS version, and try to send a notification again after killing your app. The notification should arrive once your app is whitelisted on Oppo and Samsung devices.

SatishKumarRaizada commented 2 years ago

Thanks for prompt reply, Other service provider like Clevertap, Braze, etc... How they are handling these request, Because being in such big organisation we can not ask users to follow these steps. Did POC's in Azure notification which works fine when killed the app no such issue.

Is this limitation in Pushy.me @pushy-me

This is such great solution for Flutter framework for sending notification but not handling these small work around could be deal breaker.

Let me know if this can be handle programatically like any other normal apps(not so famous).

pushy commented 2 years ago

Hi @SatishKumarRaizada, Thanks for informing us that the issue is not present on other notification gateways such as Azure. Some follow up questions:

1) Are you sending a silent FCM "data-only" notification, or a non-silent FCM visible notification using Azure? If possible, please share the backend code used to send a notification through Azure's FCM integration which is received on your test devices when your app is killed.

2) What are the exact model names of the Samsung & Oppo devices you used to test which received notifications via Azure notification hub when the app was killed, but not via Pushy's FCM fallback delivery?

Thanks for your help in clarifying the above. If the issue is indeed not present on Azure, we will be able to resolve it on Pushy with your help.

SatishKumarRaizada commented 2 years ago

Thank you @pushy-me for prompt reply, please find the comments for the above questions

  1. We are sending non-silent notification.
  2. The model I have used for testing Pushy fall back mechanism are Samsung(Galaxy A50 with Android 11), Oppo(F9 Pro, with Android 10), device locations are India.

Important - App killed notification sent actually never delivered not even when i open the app.

Please share .APK file if you have any sample project/POC working with app killed state, happy to test with it.

Share some sample code/project, i tried the one mentioned here but it same not working Flutter sample code. followed the steps with documentation as mentioned in previous comment i am getting those success logs which should say it has successful integration of the SDK and fall back mechanism.

pushy commented 2 years ago

Hi @SatishKumarRaizada, Thanks for your patience.

Please review our response sent via e-mail to continue debugging and resolving this issue for you.

pushy commented 2 years ago

Hi @SatishKumarRaizada, We've sent multiple replies to you and your team via e-mail but they seem to be getting blocked by your e-mail server's spam filter, so we are posting the reply on GitHub so it hopefully makes it through.

After further investigation, including running your custom-provided POC project, we noticed there are a number of coding issues that are causing background notification delivery to fail.

1) First and foremost, the reason your app doesn't receive notifications in release mode is because of the missing Proguard Rules file mentioned in the docs.

Flutter comes preconfigured with ProGuard. Please make sure the following lines are present in your android/app/[proguard-rules.pro file (please create this file if it doesn't exist yet):

-dontwarn me.pushy.**
-keep class me.pushy.** { *; }
-keep class androidx.core.app.** { *; }
-keep class android.support.v4.app.** { *; }

2) Second, the backgroundNotificationListener method should be inside main.dart as well as any other Pushy SDK methods. The file home.dart in your custom project contains all the Pushy SDK commands currently, and this is the reason things aren't working as they should.

The Pushy Flutter SDK uses a Flutter background isolate to start your process and execute Dart code even after it has been killed by the user. And for this to work, it needs to be in main.dart along with the other methods as done in our demo app main.dart.

Please rectify these issues and try your test again. Alternatively, we highly recommend that you clone the Pushy Flutter Demo app and repeat the tests (it already has FCM Fallback Delivery integrated & enabled) and confirm that background notification delivery works:

  1. Clone the repository locally: git clone https://github.com/pushy-me/pushy-demo-flutter.git
  2. Run cd pushy-demo-flutter
  3. Run flutter run to run the app on either an emulator or a connected Android device
  4. Observe the Android logcat for the Pushy device token and paste it into the demo page to send yourself a test notification
  5. Swipe the app away / kill it
  6. Try sending a notification again
  7. Confirm it was received even after app killed

We can confirm that on our test Samsung device running Android 11, notifications were successfully received by the Pushy Flutter Demo project even after the app being killed.