pushy/pushy-demo-flutter

Can you help in setting up pushy foreground service?

Closed this issue · 2 comments

Hello @pushy-me .
I want to implement the foreground service for pushy. I could not find any documentation for it. Please help me set it up.

pushy commented

Hi @chinmayilondhe,
Apologies for the inconvenience, but there is currently no support for foreground notification functionality in the Pushy Flutter SDK. There is no ETA for this feature due to lack of demand.

We recommend implementing FCM fallback delivery to work around device power saving optimizations.

pushy commented

Hi @chinmayilondhe,
Thanks for your patience. It is now possible to use foreground service functionality in Flutter, by following these steps:

  1. Add the following permission in your android/app/src/main/AndroidManifest.xml, inside the <manifest> tag:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  1. Add the following dependency to your android/app/build.gradle file:
implementation 'me.pushy:sdk:1.0.85'
  1. Edit MainActivity.java located in your android/app/src/main/java/**/ folder:

Find:

public class MainActivity extends FlutterActivity {

Add below:

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

        // Set title & description for the ongoing notification
        String appName = getPackageManager().getApplicationLabel(getApplicationInfo()).toString();
        String description = "App is running";

        // 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 if a foreground notification is displayed, which indicates that the SDK is now running in foreground service mode.