Java Code Examples for com.google.firebase.messaging.RemoteMessage#getNotification()

The following examples show how to use com.google.firebase.messaging.RemoteMessage#getNotification() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MyFirebaseMessagingService.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 8 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage message) {
  RemoteMessage.Notification notification = message.getNotification();
  if (notification != null) {
    String title = notification.getTitle();
    String body = notification.getBody();
    // Post your own notification using NotificationCompat.Builder
    // or send the information to your UI
  }

  // Listing 11-31: Receiving data using the Firebase Messaging Service
  Map<String,String> data = message.getData();
  if (data != null) {
    String value = data.get("your_key");
    // Post your own Notification using NotificationCompat.Builder
    // or send the information to your UI
  }
}
 
Example 2
Source File: MyFirebaseMessagingService.java    From CloudFunctionsExample with Apache License 2.0 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    String notificationTitle = null, notificationBody = null;

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        notificationTitle = remoteMessage.getNotification().getTitle();
        notificationBody = 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.
    sendNotification(notificationTitle, notificationBody);
}
 
Example 3
Source File: PushNotification.java    From FastAccess with GNU General Public License v3.0 5 votes vote down vote up
@Override public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    if (remoteMessage.getNotification() != null) {
        NotificationHelper.notifyShort(this, remoteMessage.getNotification().getTitle(),
                remoteMessage.getNotification().getBody(), R.drawable.ic_fa);
    }
}
 
Example 4
Source File: FirebaseMessasing.java    From Saude-no-Mapa with MIT License 5 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    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());
    }
}
 
Example 5
Source File: MyFirebaseCloudMessagingService.java    From android-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.i("FirebaseMessage", "From: " + remoteMessage.getFrom());

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.i(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
        handleNotification(remoteMessage.getNotification().getTitle(),
                remoteMessage.getNotification().getBody());
    }
}
 
Example 6
Source File: MyFirebaseMessagingService.java    From SendBird-Android with MIT License 5 votes vote down vote up
/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
@Override
public void onMessageReceived(Context context, RemoteMessage remoteMessage) {
    Logger.d("From: " + remoteMessage.getFrom());
    if (remoteMessage.getData().size() > 0) {
        Logger.d( "Message data payload: " + remoteMessage.getData());
    }

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

    try {
        if (remoteMessage.getData().containsKey(StringSet.sendbird)) {
            String jsonStr = remoteMessage.getData().get(StringSet.sendbird);
            JSONObject sendBird = new JSONObject(jsonStr);
            JSONObject channel = sendBird.getJSONObject(StringSet.channel);
            String channelUrl = channel.getString(StringSet.channel_url);

            SendBird.markAsDelivered(channelUrl);
            sendNotification(context, sendBird);

        }
    } catch (JSONException e) {
        Logger.e(e);
    }
}
 
Example 7
Source File: MyFirebaseMessagingService.java    From JumpGo with Mozilla Public License 2.0 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());

            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) {
            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 8
Source File: FirebaseMessagingPluginService.java    From cordova-plugin-firebase-messaging with MIT License 5 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    FirebaseMessagingPlugin.sendNotification(remoteMessage);

    Intent intent = new Intent(ACTION_FCM_MESSAGE);
    intent.putExtra(EXTRA_FCM_MESSAGE, remoteMessage);
    broadcastManager.sendBroadcast(intent);

    if (FirebaseMessagingPlugin.isForceShow()) {
        RemoteMessage.Notification notification = remoteMessage.getNotification();
        if (notification != null) {
            showAlert(notification);
        }
    }
}
 
Example 9
Source File: MyFirebaseMessagingService.java    From GcmForMojo with GNU General Public License v3.0 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
    //handler = new Handler(Looper.getMainLooper()); // 使用应用的主消息循环


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

    if (remoteMessage.getData().size() > 0)
    {
        if(!remoteMessage.getData().containsKey("isAt")) remoteMessage.getData().put("isAt","0");
        if(!remoteMessage.getData().containsKey("senderType")) remoteMessage.getData().put("senderType","1");

        //SharedPreferences Settings = getSharedPreferences(PREF, Context.MODE_PRIVATE);
        String tokenSender = mySettings.getString("push_type","GCM");
        if(tokenSender.equals("GCM")) {
            Log.d(MYTAG, "谷歌推送: " + remoteMessage.getData());
            MessageUtil.MessageUtilDo(getApplicationContext(),remoteMessage.getData().get("msgId"),remoteMessage.getData().get("type"),remoteMessage.getData().get("senderType"),remoteMessage.getData().get("title"),remoteMessage.getData().get("message"),remoteMessage.getData().get("isAt"));
        }
    }
}
 
Example 10
Source File: MyFirebaseMessagingService.java    From kute with Apache License 2.0 5 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //************ CHECK TO SEE IF THERE IS A DATA MESSAGE *******************
    if(remoteMessage.getData().size() >0)

    {
        String messagebody;
        Log.i(TAG, "Message data payload: " + remoteMessage.getData());
        try
        {
            JSONObject data=new JSONObject(remoteMessage.getData());
            processDataMessage(data);
            //imageurl=data.getString("image");
        }
        catch (Exception e)
        {
            Log.d("Json Exception",e.toString());
            messagebody=null;
            //imageurl=null;
        }
        //sendBigNotification(messagebody);
    }

    //***************** CHECK TO SEE IF THERE IS A NOTIFICATION *********************//

    if(remoteMessage.getNotification()!=null)

    {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        Intent intent = new Intent(this, Main.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
        sendBigNotification(remoteMessage.getNotification().getBody(),pendingIntent);
    }
}
 
Example 11
Source File: MyFirebaseMessagingService.java    From BOMBitUP with GNU General Public License v3.0 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());

        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) {
        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 12
Source File: MessagingService.java    From cordova-plugin-firebase-extended-notification with MIT License 5 votes vote down vote up
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    if(remoteMessage.getNotification() != null
        || remoteMessage.getData() == null
        || remoteMessage.getData().get("notificationOptions") == null){
        return; //does nothing
    }
    Map<String, String> originalData = remoteMessage.getData();
    JSONObject options;
    try {
        // If as object
        options = new JSONObject(originalData.get("notificationOptions"));
    } catch (JSONException e1) {
        try {
            // If stringified
            String notificationOptions = originalData.get("notificationOptions");
            if(notificationOptions == null || notificationOptions.length() < 2) {
                // Should not substring on small string
                return;
            }
            options = new JSONObject(notificationOptions.substring(1, notificationOptions.length() - 1));
        } catch (JSONException e2) {
            return; //invalid json, all will be as default
        }
    }
    new Manager(this).showNotification(new JSONObject(originalData), options);
}
 
Example 13
Source File: MessageActivity.java    From tindroid with Apache License 2.0 5 votes vote down vote up
private String readTopicNameFromIntent(Intent intent) {
    // Check if the activity was launched by internally-generated intent.
    String name = intent.getStringExtra("topic");
    if (!TextUtils.isEmpty(name)) {
        return name;
    }

    // Check if activity was launched from a background push notification.
    RemoteMessage msg = intent.getParcelableExtra("msg");
    if (msg != null) {
        RemoteMessage.Notification notification = msg.getNotification();
        if (notification != null) {
            return notification.getTag();
        }
    }

    if (TextUtils.isEmpty(name)) {
        // mTopicName is empty, so this is an external intent
        Uri contactUri = intent.getData();
        if (contactUri != null) {
            Cursor cursor = getContentResolver().query(contactUri,
                    new String[]{Utils.DATA_PID}, null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    name = cursor.getString(cursor.getColumnIndex(Utils.DATA_PID));
                }
                cursor.close();
            }
        }
    }

    return name;
}
 
Example 14
Source File: DemoFmService.java    From capillary with Apache License 2.0 5 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
  // Check if message contains a data payload.
  if (remoteMessage.getData().size() > 0) {
    Log.d(TAG, "data message received with payload: " + remoteMessage.getData());
    handleDataMessage(remoteMessage.getData());
  }

  // Check if message contains a notification payload.
  if (remoteMessage.getNotification() != null) {
    Log.d(TAG, "notification message received with body: "
        + remoteMessage.getNotification().getBody());
  }
}
 
Example 15
Source File: Firebase.java    From Android-SmartWebView with MIT License 4 votes vote down vote up
public void onMessageReceived(RemoteMessage message) {
	if (message.getNotification() != null) {
		sendMyNotification(message.getNotification().getTitle(), message.getNotification().getBody(), message.getNotification().getClickAction(), message.getData().get("uri"));
	}
}
 
Example 16
Source File: MyFirebaseMessagingService.java    From quickstart-android with Apache License 2.0 4 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());

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

    }

    // 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 17
Source File: MyMessagingService.java    From Pharmacy-Android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Timber.d("From %s", remoteMessage.getFrom());
    Timber.d("Notification Message Body: %s", remoteMessage.getNotification().getBody());

    // TODO: 22/5/16 Show notification on Order update

    Map<String, String> data = remoteMessage.getData();
    RemoteMessage.Notification notification = remoteMessage.getNotification();

    if (data.get("type").equals("ORDER_UPDATE")) {
        createOrderUpdateNotification(notification, data);
    }


}
 
Example 18
Source File: ChatFirebaseMessagingService.java    From chat21-android-sdk with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(DEBUG_NOTIFICATION, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(DEBUG_NOTIFICATION, "Message data payload: " + remoteMessage.getData());
//            example DIRECT:
//            {
//                sender=u2K7nLo2dTZEOYYTykrufN6BDF92,
//                sender_fullname=Stefano De Pascalis,
//                channel_type=direct,
//                text=Foreground,
//                timestamp=1517913805930,
//                recipient_fullname=Pinch Tozoom,
//                recipient=QetCMCeMldY06F4YPOeC6Rvph4C3
//            }
//
//            example GROUP:
//            {
//                google.sent_time=1518079821473,
//                google.ttl=2419200,
//                gcm.notification.e=1,
//                sender=QetCMCeMldY06F4YPOeC6Rvph4C3,
//                sender_fullname=Pinch Tozoom,
//                gcm.notification.sound=default,
//                gcm.notification.title=Pinch Tozoom,
//                channel_type=group,
//                from=77360455507,
//                text=Foreground group,
//                gcm.notification.sound2=default,
//                timestamp=1518079821455,
//                google.message_id=0:1518079821483572%90a182f990a182f9,
//                recipient_fullname=Meeting 8 Feb 2018,
//                gcm.notification.body=Foreground group,
//                gcm.notification.icon=ic_notification_small,
//                recipient=-L4oY__34pvOXBfgeTak,
//                gcm.notification.click_action=NEW_MESSAGE,
//                collapse_key=chat21.android.demo
//            }

            String sender = remoteMessage.getData().get("sender");
            String senderFullName = remoteMessage.getData().get("sender_fullname");
            String channelType = remoteMessage.getData().get("channel_type");
            String text = remoteMessage.getData().get("text");
            String timestamp = remoteMessage.getData().get("timestamp");
            String recipientFullName = remoteMessage.getData().get("recipient_fullname");
            String recipient = remoteMessage.getData().get("recipient");

            String currentOpenConversationId = ChatManager.getInstance()
                    .getConversationsHandler()
                    .getCurrentOpenConversationId();

            if (channelType.equals(Message.DIRECT_CHANNEL_TYPE)) {

                if(StringUtils.isValid(currentOpenConversationId) && !currentOpenConversationId.equals(sender)) {
                    sendDirectNotification(sender, senderFullName, text, channelType);
                } else {
                    if(!StringUtils.isValid(currentOpenConversationId)) {
                        sendDirectNotification(sender, senderFullName, text, channelType);
                    }
                }
            } else if (channelType.equals(Message.GROUP_CHANNEL_TYPE)) {
                if(StringUtils.isValid(currentOpenConversationId) && !currentOpenConversationId.equals(recipient)) {
                    sendGroupNotification(recipient, recipientFullName, senderFullName, text, channelType);
                } else {
                    if(!StringUtils.isValid(currentOpenConversationId)) {
                        sendGroupNotification(recipient, recipientFullName, senderFullName, text, channelType);
                    }
                }
            } else {
                // default case
                if(StringUtils.isValid(currentOpenConversationId) && !currentOpenConversationId.equals(sender)) {
                    sendDirectNotification(sender, senderFullName, text, channelType);
                } else {
                    if(!StringUtils.isValid(currentOpenConversationId)) {
                        sendDirectNotification(sender, senderFullName, text, channelType);
                    }
                }
            }
        }

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

        // 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 19
Source File: MyFirebaseMessagingService.java    From SendBird-Android with MIT License 4 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());
    }

    String channelUrl = null;
    try {
        JSONObject sendBird = new JSONObject(remoteMessage.getData().get("sendbird"));
        JSONObject channel = (JSONObject) sendBird.get("channel");
        channelUrl = (String) channel.get("channel_url");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    SendBird.markAsDelivered(channelUrl);
    // 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.
    sendNotification(this, remoteMessage.getData().get("message"), channelUrl);
}