Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions syncmanager/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,14 @@
<activity android:name="com.sendbird.syncmanager.sample.groupchannel.GroupChannelActivity"
android:windowSoftInputMode="stateHidden" />

<service android:name="com.sendbird.syncmanager.sample.fcm.MyFirebaseMessagingService">
<service
android:name="com.sendbird.syncmanager.sample.fcm.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="com.sendbird.syncmanager.sample.fcm.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.sendbird.syncmanager.sample.fileprovider"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,50 +29,34 @@
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.sendbird.syncmanager.sample.R;
import com.sendbird.syncmanager.sample.groupchannel.GroupChannelActivity;
import com.sendbird.syncmanager.sample.utils.PreferenceUtils;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.sendbird.android.SendBird;
import com.sendbird.android.SendBirdException;

import org.json.JSONException;
import org.json.JSONObject;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

/**
* 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());

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

// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
Log.d("FirebaseMsgService", "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());
Log.d("FirebaseMsgService", "Message Notification Body: " + remoteMessage.getNotification().getBody());
}

String channelUrl = null;
Expand All @@ -87,32 +71,49 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
// message, here is where that should be initiated. See sendNotification method below.
sendNotification(this, remoteMessage.getData().get("message"), channelUrl);
}
// [END receive_message]

/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
public static void sendNotification(Context context, String messageBody, String channelUrl) {

@Override
public void onNewToken(String token) {
Log.d("FirebaseMsgService", "Refreshed token: " + token);

sendRegistrationToServer(token);
}

private void sendRegistrationToServer(String token) {
SendBird.registerPushTokenForCurrentUser(token, new SendBird.RegisterPushTokenWithStatusHandler() {
@Override
public void onRegistered(SendBird.PushTokenRegistrationStatus pushTokenRegistrationStatus, SendBirdException e) {
if (e != null) {
Toast.makeText(MyFirebaseMessagingService.this, "" + e.getCode() + ":" + e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}

if (pushTokenRegistrationStatus == SendBird.PushTokenRegistrationStatus.PENDING) {
Toast.makeText(MyFirebaseMessagingService.this, R.string.register_push_token, Toast.LENGTH_SHORT).show();
}
}
});
}

private void sendNotification(Context context, String messageBody, String channelUrl) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

final String CHANNEL_ID = "CHANNEL_ID";
if (Build.VERSION.SDK_INT >= 26) { // Build.VERSION_CODES.O
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, "CHANNEL_NAME", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
}

Intent intent = new Intent(context, GroupChannelActivity.class);
intent.putExtra("groupChannelUrl", channelUrl);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.img_notification)
.setColor(Color.parseColor("#7469C4")) // small icon background color
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.img_notification_large))
.setColor(Color.parseColor("#4c679c"))
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher_round))
.setContentTitle(context.getResources().getString(R.string.app_name))
.setAutoCancel(true)
.setSound(defaultSoundUri)
Expand All @@ -123,9 +124,9 @@ public static void sendNotification(Context context, String messageBody, String
if (PreferenceUtils.getNotificationsShowPreviews()) {
notificationBuilder.setContentText(messageBody);
} else {
notificationBuilder.setContentText("Somebody sent you a message.");
notificationBuilder.setContentText(getResources().getString(R.string.message_received));
}

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
notificationManager.notify(0, notificationBuilder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ void bind(final Context context, int position, final GroupChannel channel,
// If someone in the channel is typing, display the typing indicator.
if (channel.isTyping()) {
typingIndicatorContainer.setVisibility(View.VISIBLE);
lastMessageText.setText(("Someone is typing"));
lastMessageText.setText(R.string.typing_indicator);
} else {
// Display typing indicator only when someone is typing
typingIndicatorContainer.setVisibility(View.GONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,25 +238,27 @@ private void showChannelOptionsDialog(final GroupChannel channel) {
final boolean pushCurrentlyEnabled = channel.isPushEnabled();

options = pushCurrentlyEnabled
? new String[]{"Leave channel", "Turn push notifications OFF"}
: new String[]{"Leave channel", "Turn push notifications ON"};
? new String[]{getResources().getString(R.string.leave_channel),
getResources().getString(R.string.set_notification_off)}
: new String[]{getResources().getString(R.string.leave_channel),
getResources().getString(R.string.set_notification_on)};

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Channel options")
builder.setTitle(R.string.channel_options)
.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
// Show a dialog to confirm that the user wants to leave the channel.
new AlertDialog.Builder(getActivity())
.setTitle("Leave channel " + channel.getName() + "?")
.setPositiveButton("Leave", new DialogInterface.OnClickListener() {
.setTitle(String.format(getResources().getString(R.string.leave_channel_question) + " ", channel.getName()))
.setPositiveButton(R.string.all_leave, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
leaveChannel(channel);
}
})
.setNegativeButton("Cancel", null)
.setNegativeButton(R.string.cancel, null)
.create().show();
} else if (which == 1) {
setChannelPushPreferences(channel, !pushCurrentlyEnabled);
Expand All @@ -278,14 +280,15 @@ private void setChannelPushPreferences(final GroupChannel channel, final boolean
public void onResult(SendBirdException e) {
if (e != null) {
e.printStackTrace();
Toast.makeText(getActivity(), "Error: " + e.getMessage(), Toast.LENGTH_SHORT)
Toast.makeText(getActivity(), getResources().getString(R.string.error)+ " " +
e.getMessage(), Toast.LENGTH_SHORT)
.show();
return;
}

String toast = on
? "Push notifications have been turned ON"
: "Push notifications have been turned OFF";
int toast = on
? R.string.notification_on
: R.string.notification_off;

Toast.makeText(getActivity(), toast, Toast.LENGTH_SHORT)
.show();
Expand Down
Loading