我在https://www.sinch.com/tutorials/android-messaging-tutorial-using-sinch-and-parse/#message上一直关注本教程,但我在将邮件发送给其他用户时遇到了问题。当我写一条消息并发送它。我不停地收到一条祝贺短信,上面写着"消息无法发送"。
这是我的MessageService活动:
var request = new XMLHttpRequest();
request.open("GET", "/path/to/pdf", true);
request.responseType = "blob";
request.onload = function (e) {
if (this.status === 200) {
// `blob` response
console.log(this.response);
// create `objectURL` of `this.response` : `.pdf` as `Blob`
var file = window.URL.createObjectURL(this.response);
var a = document.createElement("a");
a.href = file;
a.download = this.response.name || "detailPDF";
document.body.appendChild(a);
a.click();
// remove `a` following `Save As` dialog,
// `window` regains `focus`
window.onfocus = function () {
document.body.removeChild(a)
}
};
};
request.send();
这是我的MessagingActivity:
public class MessageService extends Service implements SinchClientListener {
private static final String APP_KEY = "xxxx";
private static final String APP_SECRET = "yyyy";
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 Intent broadcastIntent = new Intent("com.jordanpeterson.textly.messages.ListUsersActivity");
private LocalBroadcastManager broadCaster;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// get the current user id from parse
currentUserId = ParseUser.getCurrentUser().getObjectId();
if (currentUserId != null && !isSinchClientStarted()) {
startSinchClient(currentUserId);
}
broadCaster = LocalBroadcastManager.getInstance(this);
return super.onStartCommand(intent, flags, startId);
}
private void startSinchClient(String userame) {
sinchClient = Sinch.getSinchClientBuilder().context(this)
.userId(userame).applicationKey(APP_KEY)
.applicationSecret(APP_SECRET).environmentHost(ENVIRONMENT)
.build();
// this client listener requires that you define
// a few methods below
sinchClient.addSinchClientListener(this);
// Messaging is "turned-on", but calling is not
sinchClient.setSupportMessaging(true);
sinchClient.setSupportActiveConnectionInBackground(true);
sinchClient.checkManifest();
sinchClient.start();
}
private boolean isSinchClientStarted() {
return sinchClient != null && sinchClient.isStarted();
}
// The next 5 methods are for the sinch client listener
@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 void onRegistrationCredentialsRequired(SinchClient client,
ClientRegistration clientRegistration) {
// No code in here yet
}
@Override
public void onLogMessage(int level, String area, String message) {
// No code in here yet either
}
@Override
public IBinder onBind(Intent intent) {
return serviceInterface;
}
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() {
sinchClient.stopListeningOnActiveConnection();
sinchClient.terminate();
}
// Public interface for ListUsersActivity & MessagingActivity
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();
}
}
}
这是我的ListUsersActivity:
private String mRecipientId;
private EditText mMessageBodyField;
private String mMessageBody;
private MessageService.MessageServiceInterface messageService;
private String mCurrentUserId;
private ServiceConnection serviceConnection = new MyServiceConnection();
private MessageClientListener messageClientListener = new MyMessageClientListener();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messaging_sinch);
bindService(new Intent(this, MessageService.class), serviceConnection,
BIND_AUTO_CREATE);
// Get RecipientId from the intent
Intent intent = getIntent();
mRecipientId = intent.getStringExtra("RECIPIENT_ID");
mCurrentUserId = ParseUser.getCurrentUser().getObjectId();
mMessageBodyField = (EditText) findViewById(R.id.messageBodyField);
// Listen for a click on the send button
findViewById(R.id.sendButton).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
// Send The Message!
mMessageBody = mMessageBodyField.getText().toString();
if (mMessageBody.isEmpty()) {
Toast.makeText(MessagingActivity.this,
"Please enter a message", Toast.LENGTH_LONG)
.show();
return;
}
messageService.sendMessage(mRecipientId, mMessageBody);
mMessageBodyField.setText("");
}
});
}
// Unbind the service when the activity is destroyed
@Override
protected void onDestroy() {
messageService.removeMessageClientListener(messageClientListener);
unbindService(serviceConnection);
super.onDestroy();
}
private class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName componentName,
IBinder iBinder) {
messageService = (MessageService.MessageServiceInterface) iBinder;
messageService.addMessageClientListener(messageClientListener);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
messageService = null;
}
}
private class MyMessageClientListener implements MessageClientListener {
@Override
public void onMessageFailed(MessageClient client, Message message,
MessageFailureInfo failureInfo) {
Toast.makeText(MessagingActivity.this, "Message failed to send.",
Toast.LENGTH_LONG).show();
}
@Override
public void onIncomingMessage(MessageClient client, Message message) {
// Display an incoming message
}
@Override
public void onMessageSent(MessageClient client, Message message,
String recipientId) {
// Display the message that was just sent
// Later, I'll show you how to store the
// Message in Parse, so you can retrieve and
// display them every time the conversation is opened
}
@Override
public void onMessageDelivered(MessageClient client,
MessageDeliveryInfo deliveryInfo) {
}
// Don't worry about this right now
@Override
public void onShouldSendPushData(MessageClient client, Message message,
List<PushPair> pushPairs) {
}
}
}
答案 0 :(得分:1)
尝试打印邮件错误,因为您可能有多种原因。 在onMessageFailed函数下,添加此项以查看失败消息:
Toast.makeText(MessagingActivity.this,failureInfo.getSinchError().getMessage(), Toast.LENGTH_LONG).show();