Back to project page android-messaging-push-tutorial.
The source code is released under:
MIT License
If you think the Android project android-messaging-push-tutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.sinch.messagingtutorial.app; /*www.j a v a 2s . co m*/ import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import com.parse.ParseUser; import com.sinch.android.rtc.ClientRegistration; import com.sinch.android.rtc.Sinch; import com.sinch.android.rtc.SinchClient; import com.sinch.android.rtc.SinchClientListener; import com.sinch.android.rtc.SinchError; import com.sinch.android.rtc.messaging.MessageClient; import com.sinch.android.rtc.messaging.MessageClientListener; import com.sinch.android.rtc.messaging.WritableMessage; public class MessageService extends Service implements SinchClientListener { private static final String APP_KEY = "1955717f-c9df-4889-a663-6963e992f65b"; private static final String APP_SECRET = "p0ppyOPp7EiFnq+TSZi8xg=="; private static final String ENVIRONMENT = "sandbox.sinch.com"; private final MessageServiceInterface serviceInterface = new MessageServiceInterface(); private SinchClient sinchClient = null; private MessageClient messageClient = null; private String currentUserId; private LocalBroadcastManager broadcaster; private Intent broadcastIntent = new Intent("com.sinch.messagingtutorial.app.ListUsersActivity"); private String regId; @Override public int onStartCommand(Intent intent, int flags, int startId) { currentUserId = ParseUser.getCurrentUser().getObjectId(); regId = intent.getStringExtra("regId"); if (currentUserId != null && !isSinchClientStarted()) { startSinchClient(currentUserId); } broadcaster = LocalBroadcastManager.getInstance(this); return super.onStartCommand(intent, flags, startId); } public void startSinchClient(String username) { sinchClient = Sinch.getSinchClientBuilder().context(this).userId(username).applicationKey(APP_KEY) .applicationSecret(APP_SECRET).environmentHost(ENVIRONMENT).build(); sinchClient.addSinchClientListener(this); sinchClient.setSupportMessaging(true); sinchClient.setSupportActiveConnectionInBackground(true); sinchClient.setSupportPushNotifications(true); sinchClient.checkManifest(); sinchClient.start(); sinchClient.registerPushNotificationData(regId.getBytes()); } private boolean isSinchClientStarted() { return sinchClient != null && sinchClient.isStarted(); } @Override public void onClientFailed(SinchClient client, SinchError error) { broadcastIntent.putExtra("success", false); broadcaster.sendBroadcast(broadcastIntent); sinchClient = null; } @Override public void onClientStarted(SinchClient client) { broadcastIntent.putExtra("success", true); broadcaster.sendBroadcast(broadcastIntent); client.startListeningOnActiveConnection(); messageClient = client.getMessageClient(); } @Override public void onClientStopped(SinchClient client) { sinchClient = null; } @Override public IBinder onBind(Intent intent) { return serviceInterface; } @Override public void onLogMessage(int level, String area, String message) { } @Override public void onRegistrationCredentialsRequired(SinchClient client, ClientRegistration clientRegistration) { } public void sendMessage(String recipientUserId, String textBody) { if (messageClient != null) { WritableMessage message = new WritableMessage(recipientUserId, textBody); messageClient.send(message); } } public void addMessageClientListener(MessageClientListener listener) { if (messageClient != null) { messageClient.addMessageClientListener(listener); } } public void removeMessageClientListener(MessageClientListener listener) { if (messageClient != null) { messageClient.removeMessageClientListener(listener); } } @Override public void onDestroy() { if (ParseUser.getCurrentUser() == null) { sinchClient.unregisterPushNotificationData(); } sinchClient.stopListeningOnActiveConnection(); sinchClient.terminate(); } public class MessageServiceInterface extends Binder { public void sendMessage(String recipientUserId, String textBody) { MessageService.this.sendMessage(recipientUserId, textBody); } public void addMessageClientListener(MessageClientListener listener) { MessageService.this.addMessageClientListener(listener); } public void removeMessageClientListener(MessageClientListener listener) { MessageService.this.removeMessageClientListener(listener); } public boolean isSinchClientStarted() { return MessageService.this.isSinchClientStarted(); } } }