Created
July 4, 2018 09:21
-
-
Save kyawhtut-cu/1c04dd0467b6799ab38282d3babc4fa0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService { | |
private NotificationManager notificationManager; | |
private int NOTIFICATION_ID; | |
public static final String ANDROID_CHANNEL_ID = "com.kyawhtut.firebaseandwebtesting.ANDROID"; | |
@Override | |
public void onMessageReceived(RemoteMessage remoteMessage) { | |
String message = remoteMessage.getData().get("message"); | |
String imageUri = remoteMessage.getData().get("image"); | |
showNotification(message, imageUri); | |
} | |
private void showNotification(String message, String image) { | |
Log.i("Message", message); | |
Intent intent = new Intent(this, MainActivity.class); | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, | |
PendingIntent.FLAG_ONE_SHOT); | |
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); | |
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, ANDROID_CHANNEL_ID) | |
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.place_holder))/*Notification icon image*/ | |
.setSmallIcon(R.mipmap.ic_launcher) | |
.setContentTitle(message) | |
.setStyle(new NotificationCompat.BigPictureStyle() | |
.bigPicture(BitmapFactory.decodeResource(this.getResources(), R.drawable.place_holder)))/*Notification with Image*/ | |
.setAutoCancel(true) | |
.setSound(defaultSoundUri) | |
.setContentIntent(pendingIntent); | |
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { | |
createNotificationChannel(notificationManager); | |
} | |
NOTIFICATION_ID = (int) Math.random(); | |
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()); | |
fetchBitmapFromURLAsync(image, notificationBuilder); | |
} | |
@RequiresApi(api = Build.VERSION_CODES.O) | |
private void createNotificationChannel(NotificationManager mNotificationManager) { | |
if (mNotificationManager.getNotificationChannel(ANDROID_CHANNEL_ID) == null) { | |
NotificationChannel notificationChannel = | |
new NotificationChannel(ANDROID_CHANNEL_ID, | |
this.getString(R.string.app_name), | |
NotificationManager.IMPORTANCE_LOW); | |
notificationChannel.setDescription( | |
this.getString(R.string.app_name)); | |
mNotificationManager.createNotificationChannel(notificationChannel); | |
} | |
} | |
private void fetchBitmapFromURLAsync(final String imageUrl, final NotificationCompat.Builder builder) { | |
Log.i("Fetch Image : ", imageUrl); | |
GlideApp.with(this) | |
.asBitmap() | |
.load(imageUrl) | |
.into(new SimpleTarget<Bitmap>() { | |
@Override | |
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { | |
Log.i("Resource Ready","true"); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment