Guest User

Untitled

a guest
Feb 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService implements Constants{
  2.  
  3. SharedPreferences sp;
  4. @Override
  5. public void onMessageReceived(RemoteMessage remoteMessage) {
  6. sp = getSharedPreferences(CHECK_SETTINGS,
  7. Context.MODE_PRIVATE);
  8.  
  9. boolean webSite = sp.getBoolean(notifWebSite, true);
  10. boolean shops = sp.getBoolean(notifShops, true);
  11. if(webSite && shops){
  12. if(remoteMessage.getData().equals("WEB")){
  13. sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getData().get("WEB"));
  14. }
  15. if (remoteMessage.getData().equals("SHOP")){
  16. sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getData().get("SHOP"));
  17. }
  18. }
  19. else if(!webSite && !shops){
  20.  
  21. }
  22. else if(!webSite && shops){
  23. if(remoteMessage.getData().equals("SHOP")){
  24. sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getData().get("WEB"));
  25. }
  26. }
  27. else if(webSite && !shops){
  28. if(remoteMessage.getData().equals("WEB")){
  29. sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getData().get("WEB"));
  30. }
  31. }
  32.  
  33.  
  34.  
  35. /*if(remoteMessage.getData().get("URL")==null){
  36. sendNotification(remoteMessage.getNotification().getBody(),URL_HODITE_COM);
  37. }
  38. else{
  39. sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getData().get("URL"));
  40. }*/
  41.  
  42. }
  43.  
  44. private void sendNotification(String body,String url) {
  45. Intent intent=new Intent(this,WebActivity.class);
  46. intent.putExtra(KEY_INTENT,url);
  47.  
  48. PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
  49.  
  50. Notification.Builder builder = new Notification.Builder(this);
  51. // оставим только самое необходимое
  52. builder.setContentIntent(pendingIntent)
  53. .setWhen(System.currentTimeMillis()) //Время уведомления
  54. .setSmallIcon(R.mipmap.ico)
  55. .setContentTitle("Hodite")
  56. .setContentText(body); // Текст уведомления
  57.  
  58. Notification notification = builder.build();
  59. notification.defaults = Notification.DEFAULT_SOUND |
  60. Notification.DEFAULT_VIBRATE;
  61. // ставим флаг, чтобы уведомление пропало после нажатия
  62. notification.flags |= Notification.FLAG_AUTO_CANCEL;
  63. NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  64. nm.notify((body+url).hashCode(),notification);
  65. }
  66.  
  67.  
  68. }
Add Comment
Please, Sign In to add comment