Guest User

Untitled

a guest
Aug 19th, 2019
71
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.notificationapp.Service;
  2.  
  3. import android.app.NotificationChannel;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.app.TaskStackBuilder;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.graphics.Bitmap;
  10. import android.graphics.drawable.Drawable;
  11. import android.net.Uri;
  12. import android.os.Build;
  13. import android.os.Handler;
  14. import android.os.Looper;
  15. import android.support.v4.app.NotificationCompat;
  16. import android.text.TextUtils;
  17. import android.util.Log;
  18.  
  19. import com.example.notificationapp.R;
  20. import com.google.firebase.messaging.FirebaseMessagingService;
  21. import com.google.firebase.messaging.RemoteMessage;
  22. import com.squareup.picasso.Picasso;
  23. import com.squareup.picasso.Target;
  24.  
  25. import java.util.Random;
  26.  
  27. public class MyFirebaseService extends FirebaseMessagingService {
  28.  
  29. @Override
  30. public void onNewToken(String s) {
  31. super.onNewToken(s);
  32. Log.d("TOKEN_UPDATE",s);
  33. }
  34.  
  35.  
  36.  
  37. @Override
  38. public void onMessageReceived(RemoteMessage remoteMessage) {
  39. super.onMessageReceived(remoteMessage);
  40. if(remoteMessage.getNotification() != null)
  41. {
  42. final String title = remoteMessage.getNotification().getTitle();
  43. final String body = remoteMessage.getNotification().getBody();
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. String url ="";
  54. if (remoteMessage.getData() != null)
  55. url = remoteMessage.getData().get("img_url");
  56. if(!TextUtils.isEmpty(url)){
  57.  
  58. final String finalUrl = url;
  59.  
  60. new Handler(Looper.getMainLooper())
  61. .post(new Runnable() {
  62. @Override
  63. public void run() {
  64. Picasso.get()
  65. .load(finalUrl)
  66. .into(new Target() {
  67. @Override
  68. public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
  69. showNotification(MyFirebaseService.this,
  70. title,body,null,bitmap);
  71. }
  72.  
  73. @Override
  74. public void onBitmapFailed(Exception e, Drawable errorDrawable) {
  75.  
  76. }
  77.  
  78. @Override
  79. public void onPrepareLoad(Drawable placeHolderDrawable) {
  80.  
  81. }
  82. });
  83. }
  84. });
  85.  
  86. }
  87. else
  88. showNotification(MyFirebaseService.this,title,body,null,null);
  89.  
  90. }
  91. else {
  92. final String title = remoteMessage.getData().get("title");
  93. final String body = remoteMessage.getData().get("body");
  94. String url = remoteMessage.getData().get("img_url");
  95. if(!TextUtils.isEmpty(url)){
  96.  
  97. final String finalUrl = url;
  98.  
  99. new Handler(Looper.getMainLooper())
  100. .post(new Runnable() {
  101. @Override
  102. public void run() {
  103. Picasso.get()
  104. .load(finalUrl)
  105. .into(new Target() {
  106. @Override
  107. public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
  108. showNotification(MyFirebaseService.this,
  109. title,body,null,bitmap);
  110. }
  111.  
  112. @Override
  113. public void onBitmapFailed(Exception e, Drawable errorDrawable) {
  114.  
  115. }
  116.  
  117. @Override
  118. public void onPrepareLoad(Drawable placeHolderDrawable) {
  119.  
  120. }
  121. });
  122. }
  123. });
  124.  
  125. }
  126. else
  127. showNotification(MyFirebaseService.this,title,body,null,null);
  128. }
  129. }
  130.  
  131.  
  132. private void showNotification(Context context,
  133. String title,
  134. String body,
  135. Intent pendingIntent,
  136. Bitmap bitmap)
  137. {
  138. NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
  139.  
  140. int notificationId = new Random().nextInt();
  141.  
  142. String channelId = "";
  143. String channelName = "";
  144.  
  145. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
  146. NotificationChannel channel = new NotificationChannel(channelId,channelName,notificationManager.IMPORTANCE_HIGH);
  147. notificationManager.createNotificationChannel(channel);
  148.  
  149. }
  150.  
  151. NotificationCompat.Builder builder;
  152. if(bitmap != null)
  153. builder = new NotificationCompat.Builder(context)
  154. .setSmallIcon(R.mipmap.ic_launcher_round)
  155. .setLargeIcon(bitmap)
  156. .setContentTitle(title)
  157. .setContentText(body);
  158.  
  159.  
  160. else
  161. builder = new NotificationCompat.Builder(context)
  162. .setSmallIcon(R.mipmap.ic_launcher_round)
  163. .setContentTitle(title)
  164. .setContentText(body);
  165. if(pendingIntent != null){
  166. TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
  167. stackBuilder.addNextIntent(pendingIntent);
  168. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
  169. builder.setContentIntent(resultPendingIntent);
  170. }
  171.  
  172. notificationManager.notify(notificationId, builder.build());
  173.  
  174.  
  175.  
  176. }
  177. }
RAW Paste Data