|
| 1 | +package com.urbanairship.sample; |
| 2 | + |
| 3 | +import android.app.PendingIntent; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.graphics.Bitmap; |
| 7 | +import android.os.Handler; |
| 8 | +import android.os.Looper; |
| 9 | +import android.util.Log; |
| 10 | +import android.widget.RemoteViews; |
| 11 | + |
| 12 | +import com.bumptech.glide.Glide; |
| 13 | +import com.urbanairship.json.JsonMap; |
| 14 | +import com.urbanairship.liveupdate.CallbackLiveUpdateNotificationHandler; |
| 15 | +import com.urbanairship.liveupdate.LiveUpdate; |
| 16 | +import com.urbanairship.liveupdate.LiveUpdateEvent; |
| 17 | + |
| 18 | +import java.util.concurrent.ExecutionException; |
| 19 | +import java.util.concurrent.ExecutorService; |
| 20 | +import java.util.concurrent.Executors; |
| 21 | + |
| 22 | +import androidx.annotation.MainThread; |
| 23 | +import androidx.annotation.NonNull; |
| 24 | +import androidx.annotation.Nullable; |
| 25 | +import androidx.annotation.WorkerThread; |
| 26 | +import androidx.core.app.NotificationCompat; |
| 27 | + |
| 28 | +/** |
| 29 | + * Sample sports live update handler, with support for asynchronously fetching team images during |
| 30 | + * onUpdate. |
| 31 | + */ |
| 32 | +public class SampleAsyncLiveUpdate implements CallbackLiveUpdateNotificationHandler { |
| 33 | + |
| 34 | + private final ExecutorService backgroundExecutor = Executors.newSingleThreadExecutor(); |
| 35 | + private final Handler mainHandler = new Handler(Looper.getMainLooper()); |
| 36 | + |
| 37 | + @Override |
| 38 | + public void onUpdate(@NonNull Context context, |
| 39 | + @NonNull LiveUpdateEvent event, |
| 40 | + @NonNull LiveUpdate update, |
| 41 | + @NonNull LiveUpdateResultCallback callback) { |
| 42 | + |
| 43 | + Log.d("SampleAsyncLiveUpdate", "onUpdate: action=" + event + ", update=" + update); |
| 44 | + |
| 45 | + if (event == LiveUpdateEvent.END) { |
| 46 | + // Dismiss the live update on END. The default behavior will leave the Live Update |
| 47 | + // in the notification tray until the dismissal time is reached or the user dismisses it. |
| 48 | + callback.cancel(); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + JsonMap content = update.getContent(); |
| 53 | + int teamOneScore = content.opt("team_one_score").getInt(0); |
| 54 | + int teamTwoScore = content.opt("team_two_score").getInt(0); |
| 55 | + String teamOneName = content.opt("team_one_name").getString("Foxes"); |
| 56 | + String teamTwoName = content.opt("team_two_name").getString("Tigers"); |
| 57 | + String teamOneImageUrl = content.opt("team_one_image").getString(""); |
| 58 | + String teamTwoImageUrl = content.opt("team_two_image").getString(""); |
| 59 | + String statusUpdate = content.opt("status_update").optString(); |
| 60 | + |
| 61 | + RemoteViews bigLayout = new RemoteViews(context.getPackageName(), R.layout.live_update_notification_big); |
| 62 | + bigLayout.setTextViewText(R.id.teamOneName, teamOneName); |
| 63 | + bigLayout.setTextViewText(R.id.teamTwoName, teamTwoName); |
| 64 | + bigLayout.setTextViewText(R.id.teamOneScore, String.valueOf(teamOneScore)); |
| 65 | + bigLayout.setTextViewText(R.id.teamTwoScore, String.valueOf(teamTwoScore)); |
| 66 | + bigLayout.setTextViewText(R.id.statusUpdate, statusUpdate); |
| 67 | + |
| 68 | + RemoteViews smallLayout = new RemoteViews(context.getPackageName(), R.layout.live_update_notification_small); |
| 69 | + smallLayout.setTextViewText(R.id.teamOneScore, String.valueOf(teamOneScore)); |
| 70 | + smallLayout.setTextViewText(R.id.teamTwoScore, String.valueOf(teamTwoScore)); |
| 71 | + |
| 72 | + Intent launchIntent = context.getPackageManager() |
| 73 | + .getLaunchIntentForPackage(context.getPackageName()) |
| 74 | + .addCategory(update.getName()) |
| 75 | + .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP) |
| 76 | + .setPackage(null); |
| 77 | + |
| 78 | + PendingIntent contentIntent = PendingIntent.getActivity( |
| 79 | + context, 0, launchIntent, PendingIntent.FLAG_IMMUTABLE); |
| 80 | + |
| 81 | + NotificationCompat.Builder builder = |
| 82 | + new NotificationCompat.Builder(context, "sports") |
| 83 | + .setSmallIcon(R.drawable.ic_notification) |
| 84 | + .setPriority(NotificationCompat.PRIORITY_MAX) |
| 85 | + .setCategory(NotificationCompat.CATEGORY_EVENT) |
| 86 | + .setStyle(new NotificationCompat.DecoratedCustomViewStyle()) |
| 87 | + .setCustomContentView(smallLayout) |
| 88 | + .setCustomBigContentView(bigLayout) |
| 89 | + .setContentIntent(contentIntent); |
| 90 | + |
| 91 | + if (teamOneImageUrl.isEmpty() && teamTwoImageUrl.isEmpty()) { |
| 92 | + callback.ok(builder); |
| 93 | + } else { |
| 94 | + fetchTeamIcons(context, teamOneImageUrl, teamTwoImageUrl, (teamOneBmp, teamTwoBmp) -> { |
| 95 | + if (teamOneBmp != null && teamTwoBmp != null) { |
| 96 | + smallLayout.setImageViewBitmap(R.id.teamTwoImage, teamTwoBmp); |
| 97 | + smallLayout.setImageViewBitmap(R.id.teamOneImage, teamOneBmp); |
| 98 | + bigLayout.setImageViewBitmap(R.id.teamOneImage, teamOneBmp); |
| 99 | + bigLayout.setImageViewBitmap(R.id.teamTwoImage, teamTwoBmp); |
| 100 | + } |
| 101 | + |
| 102 | + NotificationResult result = callback.ok(builder); |
| 103 | + |
| 104 | + // ... |
| 105 | + result.getNotification(); |
| 106 | + result.getNotificationId(); |
| 107 | + result.getNotificationTag(); |
| 108 | + }); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private void fetchTeamIcons(Context context, String teamOneUrl, String teamTwoUrl, TeamIconsCallback callback) { |
| 113 | + backgroundExecutor.execute(() -> { |
| 114 | + try { |
| 115 | + Bitmap teamOneBitmap = fetchBitmap(context, teamOneUrl); |
| 116 | + Bitmap teamTwoBitmap = fetchBitmap(context, teamTwoUrl); |
| 117 | + |
| 118 | + mainHandler.post(() -> callback.onIconsReady(teamOneBitmap, teamTwoBitmap)); |
| 119 | + } catch (Exception e) { |
| 120 | + mainHandler.post(() -> callback.onIconsReady(null, null)); |
| 121 | + } |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + @WorkerThread |
| 126 | + private Bitmap fetchBitmap(Context context, String url) throws ExecutionException, InterruptedException { |
| 127 | + return Glide.with(context) |
| 128 | + .asBitmap() |
| 129 | + .load(url) |
| 130 | + .submit() |
| 131 | + .get(); |
| 132 | + } |
| 133 | + |
| 134 | + private interface TeamIconsCallback { |
| 135 | + @MainThread |
| 136 | + void onIconsReady(@Nullable Bitmap teamOneBitmap, @Nullable Bitmap teamTwoBitmap); |
| 137 | + } |
| 138 | +} |
0 commit comments