Advertisement
Guest User

Test

a guest
Dec 13th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. package com.kam.den.carhelp;
  2.  
  3. import android.app.NotificationManager;
  4. import android.app.PendingIntent;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.graphics.Color;
  11. import android.media.RingtoneManager;
  12. import android.net.Uri;
  13. import android.os.Build;
  14. import android.support.v4.app.NotificationCompat;
  15. import android.support.v7.app.AlertDialog;
  16.  
  17. import com.google.firebase.messaging.FirebaseMessagingService;
  18. import com.google.firebase.messaging.RemoteMessage;
  19.  
  20. public class FBMessagingService extends FirebaseMessagingService
  21. {
  22. public FBMessagingService() { }
  23.  
  24. @Override
  25. public void onMessageReceived(RemoteMessage remoteMessage)
  26. {
  27. super.onMessageReceived(remoteMessage);
  28. // Check if message contains a data payload.
  29. if (remoteMessage.getData().size() > 0)
  30. {
  31. /* Map<String, String> params = remoteMessage.getData();
  32. JSONObject object = new JSONObject(params);
  33. sendNotification(object.toString());
  34. */
  35. // Log.d(TAG, "Message data payload: " + remoteMessage.getData());
  36. String message = null,title = null,ticker = null;
  37. message = remoteMessage.getData().get("body");
  38. title = remoteMessage.getData().get("title");
  39. ticker = remoteMessage.getData().get("ticker");
  40.  
  41. showAlertDialog(getApplication());
  42.  
  43. sendNotification(message,title,ticker);
  44.  
  45. }
  46.  
  47. // Check if message contains a notification payload.
  48. if (remoteMessage.getNotification() != null) {
  49. // Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
  50. }
  51. }
  52.  
  53. private void sendNotification(/*String messageBody*/String message, String title, String ticker)
  54. {
  55.  
  56. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
  57. {
  58. NotificationHelper notificationHelper = new NotificationHelper(this);
  59. notificationHelper.createNotification(title,message,ticker);
  60. return;
  61. }
  62.  
  63. //Выдергиваем координаты из сообщения.
  64. String splitterMessage[] = message.split(">>>");
  65. message = splitterMessage[0]; //Здесь сам текст
  66. String coordinate = splitterMessage[1]; //Здесь координаты
  67.  
  68.  
  69. Intent intent = new Intent(this, MainActivity.class);
  70. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  71. intent.putExtra("key",coordinate); //И отправляем координаты активити
  72.  
  73. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
  74.  
  75. // String channelId = getString(R.string.default_notification_channel_id);
  76. Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  77. Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_menu_send); //иконка в сообщении
  78. NotificationCompat.Builder notificationBuilder =
  79. new NotificationCompat.Builder(this)
  80. .setSmallIcon(R.drawable.ic_menu_send)//иконка в statusBar
  81. .setLargeIcon(largeIcon) //иконка в сообщении
  82. .setContentTitle(title)
  83. .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
  84. .setContentText(message)
  85. .setTicker(ticker)
  86. .setAutoCancel(true)
  87. .setSound(defaultSoundUri)
  88. .setColor(Color.parseColor("#4B8A08"))
  89. .setVibrate(new long[] { 1000, 1000,1000, 1000,1000 })
  90. .setContentIntent(pendingIntent);
  91.  
  92.  
  93. NotificationManager notificationManager =
  94. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  95.  
  96. notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
  97. }
  98.  
  99. private void showAlertDialog(Context context)
  100. {
  101. AlertDialog.Builder builder = new AlertDialog.Builder(context);
  102. builder.setTitle("Title")
  103. .setMessage("Message")
  104. // .setIcon(R.drawable.ic_android_cat)
  105. .setCancelable(false)
  106. .setNegativeButton("Cancel",
  107. new DialogInterface.OnClickListener() {
  108. public void onClick(DialogInterface dialog, int id) {
  109. dialog.cancel();
  110. }
  111. });
  112.  
  113.  
  114.  
  115.  
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement