Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.pushapp.medime.firebase;
- import android.app.Notification;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Color;
- import android.media.RingtoneManager;
- import android.os.Build;
- import android.support.v4.app.NotificationCompat;
- import android.util.Log;
- import com.google.firebase.messaging.FirebaseMessagingService;
- import com.google.firebase.messaging.RemoteMessage;
- import java.util.Map;
- import me.pushapp.medime.R;
- import me.pushapp.medime.app.home.HomeActivity_;
- public class MyFirebaseMessagingService extends FirebaseMessagingService {
- private static final String TAG = "FirebaseServiceMM";
- public static final String dispenserID = "dispenserId";
- @Override
- public void onMessageReceived(RemoteMessage remoteMessage) {
- super.onMessageReceived(remoteMessage);
- RemoteMessage.Notification notification = remoteMessage.getNotification();
- Map<String, String> data = remoteMessage.getData();
- String dispenserId = null;
- for ( String key : data.keySet() ) {
- Log.d(TAG, key);
- if(key.equals(dispenserID)){
- dispenserId = data.get(key);
- break;
- }
- }
- Log.d(TAG, "dispenserId: "+dispenserId);
- Log.d(TAG, "onMessageReceived: ::before");
- sendNotification(notification, data,dispenserId);
- }
- private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data, String dispenserId) {
- Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_notifica);
- Intent intent = new Intent(this, HomeActivity_.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- intent.putExtra(dispenserID,dispenserId);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
- Log.d(TAG, "onMessageReceived: ::before notificationBuilder");
- NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
- .setContentTitle(notification.getTitle())
- .setContentText(notification.getBody())
- .setAutoCancel(true)
- .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
- .setContentIntent(pendingIntent)
- .setContentInfo(notification.getTitle())
- .setLargeIcon(icon)
- .setColor(Color.parseColor("#4083B3"))
- .setLights(Color.parseColor("#4083B3"), 1000, 300)
- .setDefaults(Notification.DEFAULT_VIBRATE)
- .setSmallIcon(R.drawable.ic_notifica);
- Log.d(TAG, "onMessageReceived: ::after notificationBuilder");
- NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- // Il NotificationChannel è richiesto da Android O in poi
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- NotificationChannel channel = new NotificationChannel(
- getString(R.string.default_notification_channel_id),
- getString(R.string.default_notification_channel_name),
- NotificationManager.IMPORTANCE_DEFAULT
- );
- channel.setDescription("Notifiche di MediMe");
- channel.setShowBadge(true);
- channel.canShowBadge();
- channel.enableLights(true);
- channel.setLightColor(Color.parseColor("#4083B3"));
- channel.enableVibration(true);
- channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500});
- notificationManager.createNotificationChannel(channel);
- }
- notificationManager.notify(0, notificationBuilder.build());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement