Skip to content

Commit 1c7a5cb

Browse files
[BKNDLSS-26694]: Registration for push notifications on Android devices (#509)
* [BKNDLSS-26694]: Registration for push notifications on Android devices * [BKNDLSS-26694]: Add getChannelId( String channelName ) method Co-authored-by: Vladimir Yalovy <[email protected]>
1 parent fa6ee71 commit 1c7a5cb

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/com/backendless/push/BackendlessFCMService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private void fallBackMode( Context context, String message, String contentTitle,
146146
// android.os.Build.VERSION_CODES.O == 26
147147
if( android.os.Build.VERSION.SDK_INT > 25 )
148148
{
149-
final String channelId = Backendless.getApplicationIdOrDomain() + ":" + channelName;
149+
final String channelId = PushTemplateHelper.getChannelId( channelName );
150150
NotificationManager notificationManager = (NotificationManager) context.getSystemService( Context.NOTIFICATION_SERVICE );
151151
NotificationChannel notificationChannel = notificationManager.getNotificationChannel( channelId );
152152

src/com/backendless/push/PushTemplateHelper.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,20 @@ static Notification convertFromTemplate( final Context context, final AndroidPus
100100
// Notification channel ID is ignored for Android 7.1.1 (API level 25) and lower.
101101

102102
String messageText = newBundle.getString( PublishOptions.MESSAGE_TAG );
103-
103+
104104
String contentTitle = newBundle.getString( PublishOptions.ANDROID_CONTENT_TITLE_TAG );
105105
contentTitle = contentTitle != null ? contentTitle : template.getContentTitle();
106-
106+
107107
String summarySubText = newBundle.getString( PublishOptions.ANDROID_SUMMARY_SUBTEXT_TAG );
108108
summarySubText = summarySubText != null ? summarySubText : template.getSummarySubText();
109-
109+
110110
String largeIcon = newBundle.getString( PublishOptions.ANDROID_LARGE_ICON_TAG );
111111
largeIcon = largeIcon != null ? largeIcon : template.getLargeIcon();
112-
112+
113113
String attachmentUrl = newBundle.getString( PublishOptions.ANDROID_ATTACHMENT_URL_TAG );
114114
attachmentUrl = attachmentUrl != null ? attachmentUrl : template.getAttachmentUrl();
115-
116-
115+
116+
117117
NotificationCompat.Builder notificationBuilder;
118118
// android.os.Build.VERSION_CODES.O == 26
119119
if( android.os.Build.VERSION.SDK_INT > 25 )
@@ -252,7 +252,7 @@ else if( messageText.length() > 35 )
252252
.setContentTitle( contentTitle != null ? contentTitle : template.getContentTitle() )
253253
.setSubText( summarySubText != null ? summarySubText : template.getSummarySubText() )
254254
.setContentText( messageText );
255-
255+
256256
Intent notificationIntent;
257257
if (template.getActionOnTap() == null || template.getActionOnTap().isEmpty())
258258
notificationIntent = appContext.getPackageManager().getLaunchIntentForPackage(appContext.getPackageName());
@@ -261,11 +261,11 @@ else if( messageText.length() > 35 )
261261
notificationIntent = new Intent("ActionOnTap");
262262
notificationIntent.setClassName(appContext, template.getActionOnTap());
263263
}
264-
264+
265265
notificationIntent.putExtras(newBundle);
266266
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
267267
PendingIntent contentIntent = PendingIntent.getActivity(appContext, notificationId * 3, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
268-
268+
269269
// user should use messageId and tag(templateName) to cancel notification.
270270
notificationBuilder.setContentIntent(contentIntent);
271271

@@ -329,20 +329,38 @@ static public void deleteNotificationChannel( Context context )
329329

330330
NotificationManager notificationManager = (NotificationManager) context.getSystemService( Context.NOTIFICATION_SERVICE );
331331
List<NotificationChannel> notificationChannels = notificationManager.getNotificationChannels();
332-
for (NotificationChannel notifChann : notificationChannels)
333-
notificationManager.deleteNotificationChannel( notifChann.getId() );
332+
final String channelNotificationPrefix = getChannelNotificationPrefix();
333+
for( NotificationChannel notifChann : notificationChannels )
334+
{
335+
String notifChannId = notifChann.getId();
336+
// Delete NotificationChannel only if Backendless created this channel
337+
if( notifChannId.startsWith( channelNotificationPrefix ) )
338+
{
339+
notificationManager.deleteNotificationChannel( notifChannId );
340+
}
341+
}
342+
}
343+
344+
static String getChannelId( String channelName )
345+
{
346+
return getChannelNotificationPrefix() + ":" + channelName;
347+
}
348+
349+
static private String getChannelNotificationPrefix()
350+
{
351+
return Backendless.getApplicationIdOrDomain();
334352
}
335353

336354
static public NotificationChannel getNotificationChannel( final Context context, final String templateName )
337355
{
338-
final String channelId = Backendless.getApplicationIdOrDomain() + ":" + templateName;
356+
final String channelId = getChannelId( templateName );
339357
NotificationManager notificationManager = (NotificationManager) context.getSystemService( Context.NOTIFICATION_SERVICE );
340358
return notificationManager.getNotificationChannel( channelId );
341359
}
342360

343361
static public NotificationChannel getOrCreateNotificationChannel( Context context, final AndroidPushTemplate template )
344362
{
345-
final String channelId = Backendless.getApplicationIdOrDomain() + ":" + template.getName();
363+
final String channelId = getChannelId( template.getName() );
346364
NotificationManager notificationManager = (NotificationManager) context.getSystemService( Context.NOTIFICATION_SERVICE );
347365

348366
NotificationChannel notificationChannel = notificationManager.getNotificationChannel( channelId );

0 commit comments

Comments
 (0)