Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. private void showNotifications(String title, String msg, String deepLink){
  2. msg = msg.replace("<br/>", "n");
  3. Spanned htmlText = Html.fromHtml(msg);
  4. RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_push);
  5. contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher_foreground);
  6. contentView.setTextViewText(R.id.title, title);
  7. contentView.setTextViewText(R.id.text, htmlText);
  8.  
  9. Intent intent ;
  10.  
  11. if (deepLink!=null)
  12. intent = new Intent(deepLink);
  13. else
  14. intent = new Intent(this, MainActivity.class);
  15.  
  16. //get desired location from deep link
  17. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  18. intent.putExtra("title", title);
  19. intent.putExtra("message", msg);
  20. PendingIntent pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  21.  
  22.  
  23. Random rand = new Random();
  24. int notificationID = rand.nextInt(50000);
  25. Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  26.  
  27. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  28.  
  29. NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), NOTIF_CHANNEL_ID)
  30. .setContent(contentView)
  31. .setContentIntent(pendingIntent)
  32. .setSound(defaultSoundUri)
  33. .setSmallIcon(R.drawable.status_bar_icon)
  34. .setAutoCancel(true)
  35. .setChannelId(NOTIF_CHANNEL_ID);
  36.  
  37. NotificationChannel channel = new NotificationChannel(NOTIF_CHANNEL_ID, NOTIF_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
  38. channel.enableLights(true);
  39. channel.setShowBadge(true);
  40. channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
  41. channel.enableVibration(true);
  42. NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  43. manager.createNotificationChannel(channel);
  44. // manager.notify(notificationID, notification.build());
  45. manager.notify(NOTIF_CHANNEL_ID, notificationID, notification.build());
  46.  
  47. }
  48. else
  49. {
  50. Notification notification = new NotificationCompat.Builder(this)
  51. .setContent(contentView)
  52. .setContentIntent(pendingIntent)
  53. .setSmallIcon(R.drawable.status_bar_icon)
  54. .setSound(defaultSoundUri)
  55. .setAutoCancel(true)
  56. .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
  57. .build();
  58.  
  59. NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  60. manager.notify(notificationID, notification);
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement