Java Code Examples for com.google.firebase.messaging.RemoteMessage

The following are top voted examples for showing how to use com.google.firebase.messaging.RemoteMessage. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: android-mobile-engage-sdk   File: MobileEngageMessagingService.java   View source code 7 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Map<String, String> remoteData = remoteMessage.getData();

    EMSLogger.log(MobileEngageTopic.PUSH, "Remote message data %s", remoteData);

    if (MessagingServiceUtils.isMobileEngageMessage(remoteData)) {

        EMSLogger.log(MobileEngageTopic.PUSH, "RemoteMessage is ME message");

        MessagingServiceUtils.cacheNotification(remoteData);

        Notification notification = MessagingServiceUtils.createNotification(
                getApplicationContext(),
                remoteData,
                MobileEngage.getConfig().getOreoConfig());

        ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
                .notify((int) System.currentTimeMillis(), notification);
    }
}
 
Example 2
Project: social-app-android   File: MyFirebaseMessagingService.java   View source code 7 votes vote down vote up
private void parseCommentOrLike(RemoteMessage remoteMessage) {
    String notificationTitle = remoteMessage.getData().get(TITLE_KEY);
    String notificationBody = remoteMessage.getData().get(BODY_KEY);
    String notificationImageUrl = remoteMessage.getData().get(ICON_KEY);
    String postId = remoteMessage.getData().get(POST_ID_KEY);

    Intent backIntent = new Intent(this, MainActivity.class);
    Intent intent = new Intent(this, PostDetailsActivity.class);
    intent.putExtra(PostDetailsActivity.POST_ID_EXTRA_KEY, postId);

    Bitmap bitmap = getBitmapFromUrl(notificationImageUrl);

    sendNotification(notificationTitle, notificationBody, bitmap, intent, backIntent);

    LogUtil.logDebug(TAG, "Message Notification Body: " + remoteMessage.getData().get(BODY_KEY));
}
 
Example 3
Project: humaniq-android   File: FcmListenerService.java   View source code 7 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage message) {
    Map<String, String> data = message.getData();
    String category = data.get("category");
    Log.d(TAG, "notification received");

    if (category == null) {
        Log.w(TAG, "Category is null");
        return;
    }

    switch (category) {
        case "push":
            handlePushNotification(message);
            break;

        case "system":
            handleSystemNotification(message);
            break;

        default:
            Log.w(TAG, "Unknown notification category: " + category);
    }
}
 
Example 4
Project: TFG-SmartU-La-red-social   File: MyFirebaseMessagingService.java   View source code 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d(TAG, "¡Mensaje recibido!");
    //Si es un mensaje del chat de un usuario
    if(remoteMessage.getData()!=null && remoteMessage.getData().containsKey("fcm_token")) {
            String title = remoteMessage.getData().get("title");
            String message = remoteMessage.getData().get("text");
            String username = remoteMessage.getData().get("username");
            String uid = remoteMessage.getData().get("uid");
            String fcmToken = remoteMessage.getData().get("fcm_token");
            //Muestro la notifiación
            sendNotification(title, message, username, uid, fcmToken);
    }else {
        /// Si es de tipo inserción la muestro sino no.
        //Es una nueva notificación de que alguien ha creado algo
        if(remoteMessage.getData().get("accion")!=null &&
                remoteMessage.getData().get("accion").compareTo("insert")==0)
            displayNotification(remoteMessage.getNotification(), remoteMessage.getData());
        //Envío los datos al RecyclerView correspondiente para que se actualice
        addNotificacion(remoteMessage);
    }
}
 
Example 5
Project: TFG-SmartU-La-red-social   File: MyFirebaseMessagingService.java   View source code 6 votes vote down vote up
/**
 * Muestra que ha habido una nueva notificación
 * @param notification
 * @param data
 */
private void displayNotification(RemoteMessage.Notification notification, Map<String, String> data) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("notificacion","notificacion");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(notification.getTitle())
            .setContentText(notification.getBody())
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setSmallIcon(R.mipmap.ic_launch)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}
 
Example 6
Project: Clases-2017c1   File: MyFirebaseMessagingService.java   View source code 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getData().size() > 0) {
        Boolean isLongRunningTask = this.isLongRunningTask(remoteMessage.getData());

        if (isLongRunningTask) {
            scheduleJob(remoteMessage.getData());
        } else {
            handleNow(remoteMessage.getData());
        }
    }

    if (remoteMessage.getNotification() != null) {
        sendNotification(remoteMessage.getNotification().getBody());
    }
}
 
Example 7
Project: FcmNotificationHandler   File: RemoteMessageToNotificationMapper.java   View source code 6 votes vote down vote up
@Nullable
private Integer parseColor(RemoteMessage.Notification fcmNotification) {
    String notificationColor = fcmNotification.getColor();

    Integer color = null;
    if (notificationColor != null) {
        try {
            color = Color.parseColor(notificationColor);
        } catch (IllegalArgumentException ignore) {
        }
    }
    if (color == null) {
        int colorResId = getResourceIdFromApplicationMetadata("com.google.firebase.messaging.default_notification_color");
        if (colorResId != 0)
            color = ContextCompat.getColor(context, colorResId);
    }
    return color;
}
 
Example 8
Project: social-app-android   File: MyFirebaseMessagingService.java   View source code 6 votes vote down vote up
private void handleRemoteMessage(RemoteMessage remoteMessage) {
    String receivedActionType = remoteMessage.getData().get(ACTION_TYPE_KEY);
    LogUtil.logDebug(TAG, "Message Notification Action Type: " + receivedActionType);

    switch (receivedActionType) {
        case ACTION_TYPE_NEW_LIKE:
            parseCommentOrLike(remoteMessage);
            break;
        case ACTION_TYPE_NEW_COMMENT:
            parseCommentOrLike(remoteMessage);
            break;
        case ACTION_TYPE_NEW_POST:
            handleNewPostCreatedAction(remoteMessage);
            break;
    }
}
 
Example 9
Project: BookED   File: MyFirebaseMessagingService.java   View source code 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Intent i = new Intent(this, BottomNavigation.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0 , i ,
            PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder nb= new NotificationCompat.Builder(this);
    nb.setContentTitle("FCM Notification");
    nb.setContentText(remoteMessage.getNotification().getBody());
    nb.setAutoCancel(true);
    nb.setSmallIcon(R.mipmap.ic_launcher);
    nb.setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0,nb.build());
}
 
Example 10
Project: gdgApp   File: MessagingService.java   View source code 6 votes vote down vote up
private void handleMessage(RemoteMessage remoteMessage) {

        Intent intent = new Intent(this, FirebaseAuth.getInstance().getCurrentUser() == null ? LoginActivity.class:MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent intent1 = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);


        Uri defaultURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this,NotificationCompat.CATEGORY_MESSAGE)
                .setSmallIcon(R.drawable.gdg_notification_icon)
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody())
                .setAutoCancel(true)
                .setSound(defaultURI)
                .setContentIntent(intent1);

        NotificationManager manager =  ((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
        if (manager != null) {
            manager.notify(23,builder.build());
        }


    }
 
Example 11
Project: TapIn   File: MyFirebaseMessagingService.java   View source code 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
    if (remoteMessage.getData().size() > 0)
    {
        try
        {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            sendPushNotification(json);
        }
        catch (Exception e)
        {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }
}
 
Example 12
Project: Android-2017   File: FirebaseMessangingService.java   View source code 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    if (remoteMessage.getData().size() > 0) {
        // procesa los datos extra de la notificación
    }

    if (remoteMessage.getNotification() != null) {
        String title = remoteMessage.getNotification().getTitle();
        String message = remoteMessage.getNotification().getBody();
        String clickAction = remoteMessage.getNotification().getClickAction();


        Log.d(TAG, "Title: " + title);
        Log.d(TAG, "Message: " + message);
        Log.d(TAG, "clickAction: " + clickAction);

        sendNotification(title, message, clickAction);
    }

}
 
Example 13
Project: AppsTimeline   File: MessageService.java   View source code 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Map<String, String> data = remoteMessage.getData();
    if (data.size() > 0) {
        Intent resultIntent = new Intent(this, MainActivity.class);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(
                this,
                0,
                resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
        );
        // 这个是应用在前台的时候出现的通知,应用在后台不会调用,这个并不能把应用拉起来的
        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Notification n = new NotificationCompat.Builder(this, "channel.fcm")
                .setContentIntent(resultPendingIntent)
                .setContentTitle(data.get("title"))
                .setContentText(data.get("content"))
                .setSound(notificationSound)
                .setSmallIcon(R.drawable.ic_launcher)
                .build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (notificationManager != null) notificationManager.notify(1, n);
    }
}
 
Example 14
Project: 1617PROJ1Bloeddonatie-app   File: MyFirebaseMessagingService.java   View source code 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

	Log.d(TAG, "FROM:" + remoteMessage.getFrom());

	//Check if the message contains data
	if(remoteMessage.getData().size() > 0) {
		Log.d(TAG, "Message data: " + remoteMessage.getData());
	}

	//Check if the message contains notification
	if(remoteMessage.getNotification() != null) {
		Log.d(TAG, "Message body:" + remoteMessage.getNotification().getBody());
		sendNotification(remoteMessage.getNotification().getBody());
	}
}
 
Example 15
Project: CSI-KJSCEOfficial   File: CSINotificationService.java   View source code 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Notification notification = parseNotification(remoteMessage);
    updateNotifDB(notification);

    intent = new Intent(this, NotificationActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent
            .getActivity(getBaseContext(),REQUEST_CODE, intent,PendingIntent.FLAG_ONE_SHOT);
    Uri soundUri = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this,"csi_channel");
    notifBuilder
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(notification.getTitle())
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notifManager.notify(0,notifBuilder.build());
}
 
Example 16
Project: CuYemek   File: MyFirebaseMessagingService.java   View source code 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    Intent intent = new Intent(this , MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this , 0 , intent , PendingIntent.FLAG_ONE_SHOT);


    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_action_notification)
                    .setContentTitle("Cü Yemek")
                    .setContentText("Yemek Listesi Yayımlandı.")
                    .setAutoCancel(true)

                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,notificationBuilder.build());
    vib.vibrate(500);

}
 
Example 17
Project: WaJeun   File: PushService.java   View source code 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // ...

    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        Map<String, String> data = remoteMessage.getData();

        sendNotification(data.get("message"),data.get("title"));

    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.

}
 
Example 18
Project: androidadvanced   File: MyFirebaseMessagingService.java   View source code 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getNotification().getBody());
    }
}
 
Example 19
Project: firebase-fcm-sample   File: MyFirebaseMessagingService.java   View source code 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // ...

    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}
 
Example 20
Project: referendum_1o_android   File: MyFirebaseMessagingService.java   View source code 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage != null && remoteMessage.getNotification() != null) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "referendum")
                .setLargeIcon(BitmapFactory.decodeResource(
                        getResources(), R.mipmap.ic_launcher))
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(StringsManager.getString("notification_title"))
                .setContentText(remoteMessage.getNotification().getBody());

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.notify(1, notificationBuilder.build());
        }
    }
}
 
Example 21
Project: LNMOnlineAndroidSample   File: MyFirebaseMessagingService.java   View source code 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //Log.e(TAG, "From: " + remoteMessage.getFrom());

    if (remoteMessage == null)
        return;

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        //Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
        handleNotification(remoteMessage.getNotification().getBody());
    }

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        //Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            handleDataMessage(json);
        } catch (Exception e) {
            //Log.e(TAG, "Exception: " + e.getMessage());
        }
    }
}
 
Example 22
Project: underlx   File: FCMService.java   View source code 6 votes vote down vote up
private void handleDisturbanceMessage(RemoteMessage remoteMessage) {
    Map<String, String> data = remoteMessage.getData();
    if (!data.containsKey("network") || !data.containsKey("line") || !data.containsKey("disturbance")
            || !data.containsKey("status") || !data.containsKey("downtime")) {
        return;
    }

    if (new Date().getTime() - remoteMessage.getSentTime() > TimeUnit.HOURS.toMillis(5)) {
        // discard messages that have been sent more than 5 hours ago
        return;
    }

    MainService.startForDisturbanceNotification(getApplicationContext(),
            data.get("network"), data.get("line"), data.get("disturbance"), data.get("status"),
            data.get("downtime").equals("true"), remoteMessage.getSentTime());
}
 
Example 23
Project: FCM-toolbox   File: Payload.java   View source code 6 votes vote down vote up
@NonNull
public static Payload with(RemoteMessage message) {
    Map<String, String> data = message.getData();
    Set<Map.Entry<String, String>> entries = data.entrySet();
    for (Map.Entry<String, String> entry : entries) {
        try {
            switch (entry.getKey()) {
                case PingPayload.KEY:
                    return PingPayload.create(message);
                case TextPayload.KEY:
                    return TextPayload.create(message);
                case LinkPayload.KEY:
                    return LinkPayload.create(message);
                case AppPayload.KEY:
                    return AppPayload.create(message);
                default:
                    break;
            }
        } catch (Exception ignored) {
        }
    }
    return RawPayload.create(message);
}
 
Example 24
Project: app_secompufscar   File: SECOMPFirebaseService.java   View source code 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO: Handle FCM messages here.
    // If the application is in the foreground handle both data and notification messages here.
    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated.
    final String msg = remoteMessage.getNotification().getBody();

    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        public void run() {
            Toast.makeText(getBaseContext(),"Acontecendo agora: "+msg,Toast.LENGTH_LONG).show();
        }
    });

}
 
Example 25
Project: friendlychat-android   File: MyFirebaseMessagingService.java   View source code 5 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // Handle data payload of FCM messages.
    Log.d(TAG, "FCM Message Id: " + remoteMessage.getMessageId());
    Log.d(TAG, "FCM Notification Message: " + remoteMessage.getNotification());
    Log.d(TAG, "FCM Data Message: " + remoteMessage.getData());
}
 
Example 26
Project: justintrain-client-android   File: AppFCMService.java   View source code 5 votes vote down vote up
private void showNotification(RemoteMessage remoteMessage) {
        String title = remoteMessage.getNotification().getTitle();
        String body  = remoteMessage.getNotification().getBody();

        Intent intent = new Intent(this, FavouriteJourneysActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);
        if ((remoteMessage.getData().get("isDebugNotification") == null) ||
                remoteMessage.getData().get("isDebugNotification").equals("false") ||
                (remoteMessage.getData().get("isDebugNotification").equals("true") && BuildConfig.DEBUG)) {
//        Bitmap notifyImage     = BitmapFactory.decodeResource(getResources(), R.drawable.ic_notification2);
//        Uri    defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .setSmallIcon(R.drawable.ic_notification2)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(body)
                            .setBigContentTitle(title)
                            .setSummaryText(body))
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent);

            if (remoteMessage.getData().get("isDiscountNotification").equals("true")) {
                ClickActionHelper.startActivity(remoteMessage.getData().get("on_notification_click_action"), null, this);
            }

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notificationBuilder.build());
        }
    }
 
Example 27
Project: Pole-Beacon-Android-SDK   File: MyFcmListenerService.java   View source code 5 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage message) {
    String from = message.getFrom();
    data = message.getData();
    if (PoleNotificationService.onMessageReceived(message, mContext)){
        generateNotification(data);
    }
}
 
Example 28
Project: aftercare-app-android   File: DCFirebaseMessagingService.java   View source code 5 votes vote down vote up
private void showNotification(RemoteMessage remoteMessage) {
    if (remoteMessage != null && remoteMessage.getNotification() != null) {
        String title = getString(R.string.txt_app_name);
        String message = "";

        Intent intent = new Intent(this, LaunchActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        if (remoteMessage.getNotification().getTitle() != null) {
            title = remoteMessage.getNotification().getTitle();
        }

        if (remoteMessage.getNotification().getBody() != null) {
            message = remoteMessage.getNotification().getBody();
        }


        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle(title)
                .setContentText(message)
                .setSmallIcon(R.drawable.notification_icon)
                .setAutoCancel(true)
                .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification))
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(DCSharedPreferences.getNotificationId(), notificationBuilder.build());
    }
}
 
Example 29
Project: social-app-android   File: MyFirebaseMessagingService.java   View source code 5 votes vote down vote up
private void handleNewPostCreatedAction(RemoteMessage remoteMessage) {
    String postAuthorId = remoteMessage.getData().get(AUTHOR_ID_KEY);
    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

    //Send notification for each users except author of post.
    if (firebaseUser != null && !firebaseUser.getUid().equals(postAuthorId)) {
        PostManager.getInstance(this.getApplicationContext()).incrementNewPostsCounter();
    }
}
 
Example 30
Project: quake-alert-android-app   File: MyFirebaseMessagingService.java   View source code 5 votes vote down vote up
/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // [START_EXCLUDE]
    // There are two types of messages data messages and notification messages. Data messages are handled
    // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
    // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
    // is in the foreground. When the app is in the background an automatically generated notification is displayed.
    // When the user taps on the notification they are returned to the app. Messages containing both notification
    // and data payloads are treated as notification messages. The Firebase console always sends notification
    // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
    // [END_EXCLUDE]

    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}
 
Example 31
Project: Tech-Jalsa   File: MyFirebaseMessagingService.java   View source code 5 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage == null)
        return;
    if (remoteMessage.getNotification() != null) {
        handleNotification(remoteMessage.getNotification().getBody());
    }
    if (remoteMessage.getData().size() > 0) {
        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            handleDataMessage(json);
        } catch (Exception e) {
        }
    }
}
 
Example 32
Project: Sem-Update   File: MyFirebaseMessagingService.java   View source code 5 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.i(TAG, "DESDE: " + remoteMessage.getFrom());

    if(remoteMessage.getData().size() > 0){
        Log.i(TAG, "Data de mensaje: " + remoteMessage.getData());
    }

    if(remoteMessage.getNotification() != null){
        Log.i(TAG, "Notificacion.body: " + remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getNotification().getBody());
    }
}
 
Example 33
Project: Guanajoven   File: FirebaseMessagingService.java   View source code 5 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //super.onMessageReceived(remoteMessage);
    Log.d("DEBUG", "Notificacion");
    Realm.init(this);
    realm = Realm.getDefaultInstance();
    showNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("body"), remoteMessage.getData().get("tag"));
}
 
Example 34
Project: gdgApp   File: MessagingService.java   View source code 5 votes vote down vote up
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
    //We will not send any payload to this device so no need to check this.

    Log.d(getClass().getSimpleName(),"Message Received from server while in foreground");

    //fixme : Make this logic backend for better results

    FirebaseDatabase.getInstance().getReference()
            .child("root")
            .child("notifications")
            .child("author")
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    String email = dataSnapshot.getValue(String.class);
                    //noinspection ConstantConditions
                    if(Objects.equals(email, FirebaseAuth.getInstance().getCurrentUser().getEmail())){
                        Log.i("MessagingService","The sender of notification is the current user... Skip the next steps");
                        notifyMessageSent();
                    }
                    else
                        handleMessage(remoteMessage);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
}
 
Example 35
Project: android-things-blinking-led   File: IoTMessagingService.java   View source code 5 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("lightsOn",
                    "true".equals(remoteMessage.getData().get("lightsOn")));
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}
 
Example 36
Project: cordova-plugin-firebase-sdk   File: MessagingService.java   View source code 5 votes vote down vote up
/**
 * Called when a message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    String title;
    String text;
    String id;
    Map<String, String> data = remoteMessage.getData();
    RemoteMessage.Notification notification = remoteMessage.getNotification();

    if (notification != null) {
        title = notification.getTitle();
        text = notification.getBody();
        id = remoteMessage.getMessageId();
    } else {
        title = data.get("title");
        text = data.get("text");
        id = data.get("id");
    }

    if (TextUtils.isEmpty(id)) {
        Random rand = new Random();
        int n = rand.nextInt(50) + 1;
        id = Integer.toString(n);
    }

    Log.i(TAG, "From: " + remoteMessage.getFrom());
    Log.i(TAG, "Notification Message id: " + id);
    Log.i(TAG, "Notification Message Title: " + title);
    Log.i(TAG, "Notification Message Body/Text: " + text);

    if (!TextUtils.isEmpty(text) || !TextUtils.isEmpty(title) || (!remoteMessage.getData().isEmpty())) {
        boolean showNotification = (Firebase.inBackground() || !MessagingComponent.hasNotificationsCallback())
                && (!TextUtils.isEmpty(text) || !TextUtils.isEmpty(title));
        sendNotification(id, title, text, data, showNotification);
    }
}
 
Example 37
Project: cleanhood   File: PushMessagingService.java   View source code 5 votes vote down vote up
/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // [START_EXCLUDE]
    // There are two types of messages data messages and notification messages. Data messages are handled
    // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
    // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
    // is in the foreground. When the app is in the background an automatically generated notification is displayed.
    // When the user taps on the notification they are returned to the app. Messages containing both notification
    // and data payloads are treated as notification messages. The Firebase console always sends notification
    // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
    // [END_EXCLUDE]

    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        sendNotification(remoteMessage.getNotification());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getNotification());
    }



    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}
 
Example 38
Project: blockvote   File: BlockvoteFirebaseMessagingService.java   View source code 5 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
	super.onMessageReceived(remoteMessage);

	String from = remoteMessage.getFrom();
	switch (from) {
		case "/topics/v1":
		case "/topics/debug":
			Map<String, String> data = remoteMessage.getData();
			process(data);
			break;
	}
}
 
Example 39
Project: Crypto-Assistant   File: FbMessagingService.java   View source code 5 votes vote down vote up
/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // [START_EXCLUDE]
    // There are two types of messages data messages and notification messages. Data messages are handled
    // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
    // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
    // is in the foreground. When the app is in the background an automatically generated notification is displayed.
    // When the user taps on the notification they are returned to the app. Messages containing both notification
    // and data payloads are treated as notification messages. The Firebase console always sends notification
    // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
    // [END_EXCLUDE]

    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
        sendNotification(remoteMessage);
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Logger.d("Message data payload: " + remoteMessage.getData());

        if (/* Check if data needs to be processed by long running job */ true) {
            // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
            scheduleJob();
        } else {
            // Handle message within 10 seconds
            handleNow();
        }

    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Logger.d("Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}
 
Example 40
Project: Crypto-Assistant   File: FbMessagingService.java   View source code 5 votes vote down vote up
/**
     * Create and show a simple notification containing the received FCM message.
     *
     */
    private void sendNotification(RemoteMessage remoteMessage) {
        //if this driver has an order still pending delivery, don't show notifs
        Intent intent = new Intent(this, MainActivity
                .class);
        Logger.d("message received");
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("message",remoteMessage.getData().get("orderId"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
//                PendingIntent.FLAG_UPDATE_CURRENT);
//                PendingIntent.FLAG_ONE_SHOT);
        Uri path = Uri.parse("android.resource://ondemandmbile.crypto_assistant/raw/alert");
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setContentTitle(remoteMessage.getData().get("title"))
                .setContentText(remoteMessage.getData().get("body"))
                .setAutoCancel(true)
                .setSound(path)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

    }