Skip to content

Commit f01332d

Browse files
authored
Merge pull request #463 from Microsoft/fix/android_8_update_notification
Fix android 8 update notification
2 parents 653a7c5 + f2f1755 commit f01332d

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

sdk/mobile-center-distribute/src/main/java/com/microsoft/azure/mobile/distribute/Distribute.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.app.Dialog;
77
import android.app.DownloadManager;
88
import android.app.Notification;
9+
import android.app.NotificationChannel;
910
import android.app.NotificationManager;
1011
import android.app.PendingIntent;
1112
import android.app.ProgressDialog;
@@ -69,6 +70,7 @@
6970
import static com.microsoft.azure.mobile.distribute.DistributeConstants.HEADER_API_TOKEN;
7071
import static com.microsoft.azure.mobile.distribute.DistributeConstants.LOG_TAG;
7172
import static com.microsoft.azure.mobile.distribute.DistributeConstants.MEBIBYTE_IN_BYTES;
73+
import static com.microsoft.azure.mobile.distribute.DistributeConstants.NOTIFICATION_CHANNEL_ID;
7274
import static com.microsoft.azure.mobile.distribute.DistributeConstants.POSTPONE_TIME_THRESHOLD;
7375
import static com.microsoft.azure.mobile.distribute.DistributeConstants.PREFERENCE_KEY_DOWNLOAD_ID;
7476
import static com.microsoft.azure.mobile.distribute.DistributeConstants.PREFERENCE_KEY_DOWNLOAD_STATE;
@@ -167,7 +169,11 @@ public class Distribute extends AbstractMobileCenterService {
167169

168170
/**
169171
* Last download progress dialog that was shown.
172+
* Android 8 deprecates this dialog but only reason is that they want us to use a non modal
173+
* progress indicator while we actually use it to be a modal dialog for forced update.
174+
* They will always keep this dialog to remain compatible but just mark it deprecated.
170175
*/
176+
@SuppressWarnings("deprecation")
171177
private ProgressDialog mProgressDialog;
172178

173179
/**
@@ -1246,8 +1252,22 @@ synchronized boolean notifyDownload(ReleaseDetails releaseDetails, Intent intent
12461252

12471253
/* Post notification. */
12481254
MobileCenterLog.debug(LOG_TAG, "Post a notification as the download finished in background.");
1249-
Notification.Builder builder = new Notification.Builder(mContext)
1250-
.setTicker(mContext.getString(R.string.mobile_center_distribute_install_ready_title))
1255+
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
1256+
Notification.Builder builder;
1257+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
1258+
1259+
/* Create or update notification channel (mandatory on Android 8 target). */
1260+
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
1261+
mContext.getString(R.string.mobile_center_distribute_notification_category),
1262+
NotificationManager.IMPORTANCE_DEFAULT);
1263+
notificationManager.createNotificationChannel(channel);
1264+
builder = new Notification.Builder(mContext, NOTIFICATION_CHANNEL_ID);
1265+
} else {
1266+
1267+
//noinspection deprecation
1268+
builder = new Notification.Builder(mContext);
1269+
}
1270+
builder.setTicker(mContext.getString(R.string.mobile_center_distribute_install_ready_title))
12511271
.setContentTitle(mContext.getString(R.string.mobile_center_distribute_install_ready_title))
12521272
.setContentText(getInstallReadyMessage())
12531273
.setSmallIcon(mContext.getApplicationInfo().icon)
@@ -1257,7 +1277,6 @@ synchronized boolean notifyDownload(ReleaseDetails releaseDetails, Intent intent
12571277
}
12581278
Notification notification = DistributeUtils.buildNotification(builder);
12591279
notification.flags |= Notification.FLAG_AUTO_CANCEL;
1260-
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
12611280
notificationManager.notify(DistributeUtils.getNotificationId(), notification);
12621281
PreferencesStorage.putInt(PREFERENCE_KEY_DOWNLOAD_STATE, DOWNLOAD_STATE_NOTIFIED);
12631282

@@ -1290,6 +1309,7 @@ private synchronized void removeDownload(long downloadId) {
12901309
/**
12911310
* Show download progress.
12921311
*/
1312+
@SuppressWarnings("deprecation")
12931313
private void showDownloadProgress() {
12941314
mProgressDialog = new ProgressDialog(mForegroundActivity);
12951315
mProgressDialog.setTitle(R.string.mobile_center_distribute_downloading_mandatory_update);
@@ -1304,6 +1324,7 @@ private void showDownloadProgress() {
13041324
/**
13051325
* Hide progress dialog and stop updating.
13061326
*/
1327+
@SuppressWarnings("deprecation")
13071328
private synchronized void hideProgressDialog() {
13081329
if (mProgressDialog != null) {
13091330
final ProgressDialog progressDialog = mProgressDialog;

sdk/mobile-center-distribute/src/main/java/com/microsoft/azure/mobile/distribute/DistributeConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ final class DistributeConstants {
135135
*/
136136
static final long POSTPONE_TIME_THRESHOLD = 24 * 60 * 60 * 1000;
137137

138+
/**
139+
* Notification channel identifier.
140+
*/
141+
static final String NOTIFICATION_CHANNEL_ID = "mobile.center.distribute";
142+
138143
/**
139144
* Base key for stored preferences.
140145
*/

sdk/mobile-center-distribute/src/main/res/values/mobile_center_distribute.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
<string name="mobile_center_distribute_install_ready_title">App update downloaded</string>
1515
<string name="mobile_center_distribute_install_ready_message">%1$s %2$s (%3$d) has been downloaded and is ready to be installed.</string>
1616
<string name="mobile_center_distribute_install">Install</string>
17+
<string name="mobile_center_distribute_notification_category">App updates</string>
1718
</resources>

sdk/mobile-center-distribute/src/test/java/com/microsoft/azure/mobile/distribute/AbstractDistributeAfterDownloadTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.microsoft.azure.mobile.distribute;
22

3+
import android.annotation.SuppressLint;
34
import android.app.DownloadManager;
45
import android.app.Notification;
56
import android.app.NotificationManager;
@@ -293,6 +294,7 @@ Cursor mockSuccessCursor() {
293294
}
294295

295296
@NonNull
297+
@SuppressLint("NewApi")
296298
Notification.Builder mockNotificationBuilderChain() throws Exception {
297299
Notification.Builder notificationBuilder = mock(Notification.Builder.class);
298300
whenNew(Notification.Builder.class).withAnyArguments().thenReturn(notificationBuilder);
@@ -301,6 +303,7 @@ Notification.Builder mockNotificationBuilderChain() throws Exception {
301303
when(notificationBuilder.setContentText(anyString())).thenReturn(notificationBuilder);
302304
when(notificationBuilder.setSmallIcon(anyInt())).thenReturn(notificationBuilder);
303305
when(notificationBuilder.setContentIntent(any(PendingIntent.class))).thenReturn(notificationBuilder);
306+
when(notificationBuilder.setChannelId(anyString())).thenReturn(notificationBuilder);
304307
return notificationBuilder;
305308
}
306309

sdk/mobile-center-distribute/src/test/java/com/microsoft/azure/mobile/distribute/DistributeDownloadTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.microsoft.azure.mobile.distribute;
22

3+
import android.annotation.SuppressLint;
34
import android.annotation.TargetApi;
45
import android.app.Activity;
56
import android.app.DownloadManager;
67
import android.app.Notification;
8+
import android.app.NotificationChannel;
79
import android.content.ActivityNotFoundException;
810
import android.content.ComponentName;
911
import android.content.DialogInterface;
@@ -709,6 +711,7 @@ public void restartDownloadCheckIsLongEnoughToAppCanGoBackgroundAgain() throws E
709711
}
710712

711713
@Test
714+
@SuppressLint("NewApi")
712715
@SuppressWarnings("deprecation")
713716
public void dontShowInstallUiIfUpgradedAfterNotification() throws Exception {
714717

@@ -732,8 +735,9 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
732735
mockSuccessCursor();
733736
Intent installIntent = mockInstallIntent();
734737
when(mPackageManager.getApplicationInfo(mContext.getPackageName(), 0)).thenReturn(mock(ApplicationInfo.class));
738+
TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.O);
735739
Notification.Builder notificationBuilder = mockNotificationBuilderChain();
736-
when(notificationBuilder.getNotification()).thenReturn(mock(Notification.class));
740+
when(notificationBuilder.build()).thenReturn(mock(Notification.class));
737741

738742
/* Make notification happen. */
739743
Distribute.getInstance().onActivityPaused(mActivity);
@@ -742,6 +746,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
742746

743747
/* Verify. */
744748
verify(mNotificationManager).notify(anyInt(), any(Notification.class));
749+
verify(mNotificationManager).createNotificationChannel(any(NotificationChannel.class));
745750
verify(mContext, never()).startActivity(installIntent);
746751

747752
/* Restart app after upgrade, discard download and check update again. */

0 commit comments

Comments
 (0)