Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. /**
  2. * Create a notification if the application is being sent to background (i.e behind
  3. * another application's Activity).
  4. *
  5. * @param context the context of the application
  6. * @param message the message to show in the notification bar
  7. */
  8. private static void generateNotification(Context context, String message) {
  9. Log.d(TAG, "Entered generateNotification()...........");
  10. //PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
  11. //if (Util.isRunningInBackground(context) || !powerManager.isScreenOn()) {
  12. long when = System.currentTimeMillis();
  13.  
  14. // This Intent will explicitly activate the notification sub-activity
  15. Intent notificationIntent = new Intent(context, NotificationActivity.class);
  16. notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  17. notificationIntent.setAction(NOTIFICATION_MESSAGE_ACTION);
  18. notificationIntent.putExtra(EXTRA_MESSAGE, message);
  19.  
  20. // get the pending Intent
  21.  
  22. PendingIntent pendingIntent = PendingIntent.getActivity(context, NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  23.  
  24. Uri notificationSoundDefaultUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  25.  
  26. Notification.Builder notificationBuilder = new Notification.Builder(context)
  27. .setTicker(message)
  28. .setContentText(message)
  29. .setAutoCancel(true)
  30. .setContentIntent(pendingIntent)
  31. .setSound(notificationSoundDefaultUri)
  32. .setWhen(when)
  33. .setSmallIcon(R.drawable.logo)
  34. .setContentTitle(context.getString(R.string.app_name));
  35.  
  36.  
  37. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
  38. Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle();
  39. bigTextStyle.bigText(message);
  40.  
  41. notificationBuilder.setStyle(bigTextStyle);
  42. }
  43.  
  44. android.app.NotificationManager notificationManager = (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  45. notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement