If your users will be okay with seeing an ongoing foreground service notification, 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" />
2) Add the following code before Pushy.listen()
in your main activity and customize the notification as desired (scroll down for Kotlin):
// 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);
Kotlin:
// 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)
Run your app and observe if a foreground notification is displayed, which indicates that the SDK is now running in foreground service mode.
Comments
0 comments
Please sign in to leave a comment.