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 background and low memory state. In turn, a fully customizable notification will be displayed at all times in the notification bar.
1) Add the following permission to your app's AndroidManifest.xml
:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<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 before Pushy.listen()
in your main activity:
Pushy.toggleForegroundService(true, this);
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 disconnectedRun your app and observe if a foreground notification is displayed, which indicates that the SDK is now running in foreground service mode.
Alternatively, you can also build and display your own notification (scroll down for Kotlin):
// 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 before Pushy.listen();
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 before Pushy.listen();
PushySocketService.setForegroundNotification(notification)
Comments
0 comments
Please sign in to leave a comment.