Consider using our foreground service functionality to work around notification delays when your app is in the background / terminated / system is low on memory.
The Pushy Android SDK will create a foreground service that the Android OS will never terminate, which will ensure notification delivery in the background and low memory state. In turn, a fully customizable notification will be displayed in the notification bar. It is also possible to instruct your users to easily hide the ongoing notification (see step 6 below).
1) Add the following permissions to your app's AndroidManifest.xml
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_REMOTE_MESSAGING" />
<service android:name="me.pushy.sdk.services.PushySocketService" android:stopWithTask="false" />
<service android:name="me.pushy.sdk.services.PushySocketService" android:foregroundServiceType= "remoteMessaging" android:stopWithTask="false" />
3) Add the following code inside your Application()
class's onCreate()
method:
Pushy.toggleForegroundService(true, this);
Note: This invocation needs to occur in your app's Application
class in order for the foreground service to be automatically started after device reboot and on app update.
If you do not currently have an Application
class defined, please create a class called MyApplication
which extends from Application
and define it in your AndroidManifest.xml
's <application>
tag by specifying android:name=".MyApplication"
. Then, override onCreate()
and add the Pushy.toggleForegroundService()
invocation inside.
Run your app and observe if a foreground notification is displayed, which indicates that the SDK is now running in foreground service mode.
4) You can customize the notification text by declaring the following string resource names:
<!-- Pushy Foreground Service -->
<string name="pushy_foreground_service_title">Pushy</string>
<string name="pushy_foreground_service_connected">Connected to the notification service</string>
<string name="pushy_foreground_service_connecting">Connecting...</string>
<string name="pushy_foreground_service_disconnected">Not connected to the notification service</string>
You can customize the notification color by declaring the following color resource in colors.xml:
<color name="pushy_foreground_service_color">#ffffff</color>
You can customize the notification icons using the following drawable resource file names:
1.ic_pushy_foreground_service_connected
- displayed when the SDK is connected2.
ic_pushy_foreground_service_connecting
- displayed when the SDK is connecting3.
ic_pushy_foreground_service_disconnected
- displayed when the SDK is disconnectedApplication()
class's onCreate()
method:// Pushy foreground service implementation
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
// 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 inside Application class
PushySocketService.setForegroundNotification(notification);
// Pushy foreground service implementation
val notificationIntent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0)
// Set title & description for the ongoing notification
val appName = packageManager.getApplicationLabel(applicationInfo).toString()
val 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) {
val channel = NotificationChannel("pushy_ongoing", appName, NotificationManager.IMPORTANCE_MIN)
channel.description = description
// Register the channel with the system
val notificationManager = getSystemService<NotificationManager>(NotificationManager::class.java)
notificationManager.createNotificationChannel(channel)
}
// Create foreground notification using pushy_ongoing notification channel (customize as necessary)
val notification: Notification = 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 inside Application class
PushySocketService.setForegroundNotification(notification)
6) Finally, you can also instruct the user on how to easily hide the ongoing notification by displaying an in-app dialog using the following code:
// Instruct user on how to easily hide the ongoing notification
new AlertDialog.Builder(this)
.setTitle("Background push notifications")
.setMessage(getPackageManager().getApplicationLabel(getApplicationInfo()).toString() + " needs to display an ongoing notification so that it can continue to receive notifications in the background.\n\nTo hide this notification, disable \"Show notifications\" in the next screen.")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Prepare notification channel config intent to allow user to easily disable the notification channel
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
// Set package to current package
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
// Set Pushy foreground notification channel ID
intent.putExtra(Settings.EXTRA_CHANNEL_ID, "pushy_foreground_service");
// Launch notification channel config activity
startActivity(intent);
}
})
.setNegativeButton("Cancel", null).show();