Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kam.den.carhelp;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Color;
- import android.media.RingtoneManager;
- import android.net.Uri;
- import android.os.Build;
- import android.support.v4.app.NotificationCompat;
- import android.support.v7.app.AlertDialog;
- import com.google.firebase.messaging.FirebaseMessagingService;
- import com.google.firebase.messaging.RemoteMessage;
- public class FBMessagingService extends FirebaseMessagingService
- {
- public FBMessagingService() { }
- @Override
- public void onMessageReceived(RemoteMessage remoteMessage)
- {
- super.onMessageReceived(remoteMessage);
- // Check if message contains a data payload.
- if (remoteMessage.getData().size() > 0)
- {
- /* Map<String, String> params = remoteMessage.getData();
- JSONObject object = new JSONObject(params);
- sendNotification(object.toString());
- */
- // Log.d(TAG, "Message data payload: " + remoteMessage.getData());
- String message = null,title = null,ticker = null;
- message = remoteMessage.getData().get("body");
- title = remoteMessage.getData().get("title");
- ticker = remoteMessage.getData().get("ticker");
- showAlertDialog(getApplication());
- sendNotification(message,title,ticker);
- }
- // Check if message contains a notification payload.
- if (remoteMessage.getNotification() != null) {
- // Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
- }
- }
- private void sendNotification(/*String messageBody*/String message, String title, String ticker)
- {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
- {
- NotificationHelper notificationHelper = new NotificationHelper(this);
- notificationHelper.createNotification(title,message,ticker);
- return;
- }
- //Выдергиваем координаты из сообщения.
- String splitterMessage[] = message.split(">>>");
- message = splitterMessage[0]; //Здесь сам текст
- String coordinate = splitterMessage[1]; //Здесь координаты
- Intent intent = new Intent(this, MainActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- intent.putExtra("key",coordinate); //И отправляем координаты активити
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
- // String channelId = getString(R.string.default_notification_channel_id);
- Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_menu_send); //иконка в сообщении
- NotificationCompat.Builder notificationBuilder =
- new NotificationCompat.Builder(this)
- .setSmallIcon(R.drawable.ic_menu_send)//иконка в statusBar
- .setLargeIcon(largeIcon) //иконка в сообщении
- .setContentTitle(title)
- .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
- .setContentText(message)
- .setTicker(ticker)
- .setAutoCancel(true)
- .setSound(defaultSoundUri)
- .setColor(Color.parseColor("#4B8A08"))
- .setVibrate(new long[] { 1000, 1000,1000, 1000,1000 })
- .setContentIntent(pendingIntent);
- NotificationManager notificationManager =
- (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
- }
- private void showAlertDialog(Context context)
- {
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- builder.setTitle("Title")
- .setMessage("Message")
- // .setIcon(R.drawable.ic_android_cat)
- .setCancelable(false)
- .setNegativeButton("Cancel",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.cancel();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement