Firebase upstream messages - android

When sending upstream message most of the times the message does not get to my server, and even when the message received to the server the onMessageSent(String msgId) function isn't called (the onMessageReceived(RemoteMessage fcmMessage) work very well).
Why the function isn't called and why do I need to send 10 upstream messages to get response from the firebase cloud messaging to my server?
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... params) {
String sendTo = SENDER_ID + "#gcm.googleapis.com";
RemoteMessage.Builder data = new RemoteMessage.Builder(sendTo);
data.addData("Hello", "World");
try {
for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
String messageID = getRandomString();
data.setMessageId(messageID);
Logger.d(TAG, "messageID: " + messageID);
FirebaseMessaging.getInstance().send(data.build());
}
} catch (Exception e) {
Logger.e(TAG, "Error sending upstream message: " + e.getMessage());
return "Error sending upstream message:" + e.getMessage();
}
return null;
}
#Override
protected void onPostExecute(String result) {
if (result != null) {
Logger.e(TAG, "send message failed: " + result);
}
}
}.execute(null, null, null);
}

Found out the problem!!!
The problem was on the server side.
Every time I send a message to the app (android), I started a new connection to the gcm server, when maintaining a continuous connection it worked great.
For the problem with the onMessageSent not called it was because, before you send the message you need to set time to live (setTtl(Time_in_seconds)) for the message.
RemoteMessage.Builder data = new RemoteMessage.Builder(mSendTo);
data.setMessageId(messageID);
data.setTtl(120);
data.addData("Hello", "World");
FirebaseMessaging.getInstance().send(data.build());

buttonUpstreamEcho.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Echo Upstream message logic");
String message = editTextEcho.getText().toString();
Log.d(TAG, "Message: " + message + ", recipient: " + token);
FirebaseMessaging.getInstance().send(new RemoteMessage.Builder(FCM_PROJECT_SENDER_ID + FCM_SERVER_CONNECTION)
.setMessageId(Integer.toString(RANDOM.nextInt()))
.addData("message", message)
.addData("action", BACKEND_ACTION_ECHO)
.build());
// To send a message to other device through the XMPP Server, you should add the
// receiverId and change the action name to BACKEND_ACTION_MESSAGE in the data
}
});
This is a sample Android project to showcase the Firebase Cloud Messaging (FCM) to manage upstream and downstream messages.
https://github.com/carlosCharz/FCMTest
This is the video in youtube that explains what it does.
https://www.youtube.com/watch?v=SEzOKSoAMG0
Hope you find it useful.

Using the builder pattern - it is always best to chain your calls to the setter methods. So my suggestion, and based on some working examples such as this one here, would be to change your code into something like this (note that I got rid of the for-loop - you can put it back if you need it, I don't see why - perhaps you were testing out?:
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... params) {
String sendTo = SENDER_ID + "#gcm.googleapis.com";
String messageID = getRandomString();
try {
FirebaseMessaging.getInstance().send(new RemoteMessage.Builder(sendTo)
.setMessageId(messageID)
.addData("my_message", "Hello, World")
.build());
} catch (Exception e) {
Logger.e(TAG, "Error sending upstream message: " + e.getMessage());
return "Error sending upstream message:" + e.getMessage();
}
return null;
}
#Override
protected void onPostExecute(String result) {
if (result != null) {
Logger.e(TAG, "send message failed: " + result);
}
}
}.execute(null, null, null);
}
I hope this helps - try it out and let me know if it works or what errors you are getting.

Related

Send FCM to user subscribed to a topic programmatically

I am creating a BloodBank app in which, when the user requests for the blood, It takes the requested Blood group and search the same in the database. It displays list of all the users who can donate to that blood group.
In the list, I have already implemented an option to message and call the user. Additionally, I want the App to send a notification to all users who have the same blood group.
For achieving this I have subscribed the user to a topic at successful login and sent him a notification but I have done this through the console.
What I want to achieve is, as a user requests the blood and while showing him the list of all users who can donate, App should also send a notification to all the users who have subscribed to that topic.
So is there any possible way I can programmatically send FCM to all the users subscribed to the same topic.
Here I'm subscribing user to a topic at successful Login:
firebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
progressDialog.dismiss();
topicSubscription();
} else {
progressDialog.dismiss();
String exception = task.getException().getMessage();
HelperClass.showSnakbarMsg(rootView, exception);
}
}
});
}
private void topicSubscription() {
FirebaseMessaging.getInstance().subscribeToTopic("Blood")
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
String msg = getString(R.string.msg_subscribed);
if (!task.isSuccessful()) {
msg = getString(R.string.msg_subscribe_failed);
} else {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}
Log.d("log", msg);
Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
This is my Firebase messaging class:
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.
*/
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}
I have read about it and many have said to hit an API for this to send FCM programmatically. But I am creating the whole app in firebase so my DataBase is also in firebase and as my database is in firebase I can't use API for one notification only like I have to manage some table in DB for that notification and for only one table I have to manage a separate DB.
So is there any way that I can send FCM programmatically to all users who have subscribed to the same topic, on successful loading of the donor list which shows the user who can donate to the requested blood group.
Thanks
You can directly send push notification directly from android, to all the devices subscribed to the topic, check out the following link how to send msgs directly from android, but in this example user is sending message one to one, to send fcm message to user subscribed to a topic, you need to change the message format as specified by fcm documentation
User App
private void latLngNotification() {
Location loc1 = new Location("");
loc1.setLatitude(Double.parseDouble(userLat));
//loc1.setLongitude();
Location loc2 = new Location("");
loc2.setLatitude(Double.parseDouble(attendanceLat));
//loc2.setLongitude();
float distanceInMeters = loc1.distanceTo(loc2);
if (distanceInMeters > 50) {
//Toast.makeText(this, "distance: " + distanceInMeters, Toast.LENGTH_SHORT).show();
sendNotification();
} else {
//Toast.makeText(this, "distance: " + distanceInMeters, Toast.LENGTH_SHORT).show();
Toast.makeText(this, "You are in home...", Toast.LENGTH_SHORT).show();
}
}
private void sendNotification() {
String TOPIC = "/topics/admin_app"; //topic has to match what the receiver subscribed to
JSONObject notification = new JSONObject();
JSONObject notifcationBody = new JSONObject();
String title = "Quarantine outside";
String message = mobileno + " User is out of his area";
try {
notifcationBody.put("title", title);
notifcationBody.put("message", message);
notification.put("to", TOPIC);
notification.put("priority", "high");
notification.put("data", notifcationBody);
} catch (JSONException e) {
Log.e(TAG, "onCreate: " + e.getMessage());
}
Notification(notification);
}
private void Notification(JSONObject notification) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("https://fcm.googleapis.com/fcm/send", notification,
response -> Log.i(TAG, "onResponse: " + response.toString()),
error -> {
Toast.makeText(GetLocationActivity.this, "Request error", Toast.LENGTH_LONG).show();
Log.i(TAG, "onErrorResponse: Didn't work");
}) {
#Override
public Map<String, String> getHeaders() {
Map<String, String> params = new HashMap<>();
params.put("Authorization", "key=AAAAwxBqu5A:APA91bERpf-1rh02jLciILt1rsLv7HRrFVulMTEAJXJ5l_JGrSHf96qXvLQV0bIROob9e3xLK4VN8tWo-zBPUL39HjxyW4MsX5nKW_NiQlZGgLDCySVwHXADlg16mpLUjgASj--bk-_W");
params.put("Content-Type", "application/json");
return params;
}
};
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
}
Admin App
FirebaseMessaging.getInstance().subscribeToTopic("admin_app");
Intent intent = getIntent();
if (intent != null) {
String userPhone = intent.getStringExtra("message");
//Toast.makeText(this, userPhone, Toast.LENGTH_SHORT).show();
message_txt.setVisibility(View.VISIBLE);
message_txt.setText(userPhone);
} else {
message_txt.setVisibility(View.GONE);
//Toast.makeText(this, "no data", Toast.LENGTH_SHORT).show();
}

how to handle chat history response in xmpp I have already send stanza in android

In my Application, i want to get the chat history from the XMPP SMACK,For it i am sending the IQPacket stanza please check below
public void getChatHistory() throws Exception{
if (connection.isAuthenticated()){
ChatHistoryIq iq = new ChatHistoryIq("query");
iq.setType(IQ.Type.set);
iq.setStanzaId(loginUser);
System.out.println("************************************** Iq is : "+iq);
/*New code*/
connection.sendIqWithResponseCallback(iq, new PacketListener() {
#Override
public void processPacket(Stanza packet) throws NotConnectedException {
CharSequence mCharSequence = (CharSequence) packet.toXML();
System.out.println("((((((((((((((((( : " + mCharSequence);
String xml=String.valueOf(mCharSequence);
loadRSSFromURL(xml);
}
});
//The listener for receiving all the packets from the peer device
connection.addPacketListener(new PacketListener() {
#Override
public void processPacket(Stanza packet) throws NotConnectedException {
Log.i("Send IQ with Response", "****** message From : " + packet.getFrom());
Log.i("Send IQ with Response", "****** message To : " + packet.getTo());
Log.i("XML is *****************************: ", String.valueOf(packet.toXML()));
}
}, new PacketFilter() {
#Override
public boolean accept(Stanza packet) {
Log.e("$$$$$$$$$$$$$$$$$$$$$$$$ CHAT HISTORY Packet Filter From : ", packet.getFrom());
Log.e("$$$$$$$$$$$$$$$$$$$$$$$$ CHAT HISTORY Packet Filter To : ", packet.getTo());
Log.i("$$$$$$$$$$$$$$$$$$$$$$$$ CHAT HISTORY Packet Filter XML: ", packet.toString());
String xml=String.valueOf(packet.toXML());
loadRSSFromURL(xml);
return true;
}
});
connection.sendPacket(iq);
//
} else{
Toast.makeText(context,"User Not Authenticate",Toast.LENGTH_LONG).show();
}
}
I am getting the history from the above code but the problem is that , we are not able to handle the chat history , we are getting it only on our LOGCAT but not able to handle it.Means we want some callback methods to handle the chat history of XMPP. So please help to handle the chat history of XMPP..
Please check the screen shot of getting the XMPP response ScreenShot

Get user status using PubNub

I have created an application that implements app to app calling using Sinch. It works only when the caller knows the name of the recipient.
To overcome this Sinch suggested to use PubNub to get the user state. They also have a tutorial here. The problem is that tutorial is old and PubNub has updated their API since. I tried to implement the functionality using their new API on my own using their docs, but it is not working or more accurately I don't know how to do it.
My current code is:
public class LoggedUsers extends Activity {
private PubNub pubNub;
String name;
private ArrayList users;
private JSONArray loggedUserList;
ListView UserList;
TextView allUsers;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_list);
allUsers = (TextView) findViewById(R.id.JSONFromPubNub);
SharedPreferences sp = getSharedPreferences("User_Details", MODE_APPEND);
try {
name = sp.getString("UserName", "");
} catch (NullPointerException e) {
}
final PNConfiguration pnc = new PNConfiguration();
pnc.setPublishKey("publish key");
pnc.setSubscribeKey("subscribe key");
pnc.setUuid(name);
pubNub = new PubNub(pnc);
users = new ArrayList<String>();
UserList = (ListView) findViewById(R.id.listView);
String user = getUserStatus();
allUsers.setText(user);
final ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), R.layout.single_item_list, users);
UserList.setAdapter(adapter);
pubNub.addListener(new SubscribeCallback() {
#Override
public void status(PubNub pubnub, PNStatus status) {
if (status.getCategory() == PNStatusCategory.PNUnexpectedDisconnectCategory) {
// This event happens when radio / connectivity is lost
HashMap <String,String> map = new HashMap();
map.put("State","Offline");
pubNub.setPresenceState().channels(Arrays.asList("CallingChannel1")).state(map).uuid(pnc.getUuid());
} else if (status.getCategory() == PNStatusCategory.PNConnectedCategory) {
// Connect event. You can do stuff like publish, and know you'll get it.
// Or just use the connected event to confirm you are subscribed for
// UI / internal notifications, etc
HashMap <String,String> map = new HashMap();
map.put("State","Online");
pubNub.setPresenceState().channels(Arrays.asList("CallingChannel1")).state(map).uuid(pnc.getUuid());
/* if (status.getCategory() == PNStatusCategory.PNConnectedCategory) {
pubnub.publish().channel("awesomeChannel").message("hello!!").async(new PNCallback<PNPublishResult>() {
#Override
public void onResponse(PNPublishResult result, PNStatus status) {
// Check whether request successfully completed or not.
if (!status.isError()) {
// Message successfully published to specified channel.
}
// Request processing failed.
else {
// Handle message publish error. Check 'category' property to find out possible issue
// because of which request did fail.
//
// Request can be resent using: [status retry];
}
}
});
}*/
} else if (status.getCategory() == PNStatusCategory.PNReconnectedCategory) {
HashMap <String,String> map = new HashMap();
map.put("State","Online");
pubNub.setPresenceState().channels(Arrays.asList("CallingChannel1")).state(map).uuid(pnc.getUuid());
// Happens as part of our regular operation. This event happens when
// radio / connectivity is lost, then regained.
} else if (status.getCategory() == PNStatusCategory.PNDecryptionErrorCategory) {
// Handle messsage decryption error. Probably client configured to
// encrypt messages and on live data feed it received plain text.
}
}
#Override
public void message(PubNub pubnub, PNMessageResult message) {
}
#Override
public void presence(PubNub pubnub, PNPresenceEventResult presence) {
}
});
}
public String getUserStatus(){
final StringBuilder allUsers = new StringBuilder();
pubNub.subscribe().channels(Arrays.asList("CallingChannel1")).withPresence().execute();
pubNub.hereNow()
// tailor the next two lines to example
.channels(Arrays.asList("CallingChannel1"))
.includeState(true)
.includeUUIDs(true)
.async(new PNCallback<PNHereNowResult>() {
#Override
public void onResponse(PNHereNowResult result, PNStatus status) {
if (status.isError()) {
// handle error
return;
}
for (PNHereNowChannelData channelData : result.getChannels().values()) {
allUsers.append("---");
allUsers.append("channel:" + channelData.getChannelName());
allUsers.append("occoupancy: " + channelData.getOccupancy());
allUsers.append("occupants:");
for (PNHereNowOccupantData occupant : channelData.getOccupants()) {
allUsers.append("uuid: " + occupant.getUuid() + " state: " + occupant.getState());
}
}
}
});
return allUsers.toString();
}
#Override
protected void onResume() {
super.onResume();
}
}
Here are my problems:
I am trying to display all the data that I receive in a textview (later it will arranged in a listview or a recycler view) but I am getting a blank screen so I am getting null from the server.
The user status should be constantly updated to know if the user changes state (online -> offline) but there seems to be no async calls made in the code so I think it will be executed only once and then the dataset is not being changed.
How can I solve my problems?
PubNub Presence
You can monitor online and state changes using PubNub Presence. When you subscribe, subscribe with presence enabled and you will get state-change, join, leave & timeout events in the presence callback.
Callback callback = new Callback() {
#Override
public void successCallback(String channel, Object message) {
System.out.println(channel + " : "
+ message.getClass() + " : " + message.toString());
// take action on the presence events here
}
#Override
public void connectCallback(String channel, Object message) {
System.out.println("CONNECT on channel:" + channel
+ " : " + message.getClass() + " : "
+ message.toString());
}
#Override
public void disconnectCallback(String channel, Object message) {
System.out.println("DISCONNECT on channel:" + channel
+ " : " + message.getClass() + " : "
+ message.toString());
}
#Override
public void reconnectCallback(String channel, Object message) {
System.out.println("RECONNECT on channel:" + channel
+ " : " + message.getClass() + " : "
+ message.toString());
}
#Override
public void errorCallback(String channel, PubnubError error) {
System.out.println("ERROR on channel " + channel
+ " : " + error.toString());
}
};
try {
pubnub.presence("my_channel", callback);
}
catch (PubnubException e) {
System.out.println(e.toString());
}
It appears Sinch is using a rather old version of the PubNub Android SDK. I would think you could still use PubNub Android SDK v4 to do what you need to do outside of Sinch SDK unless there is some explicit requirements by Sinch to use the same version of the SDK.

error in using Asynctask for GCM registration

I am using the sameple code from developer website but get error in compiling.
http://developer.android.com/google/gcm/gs.html
copying the code below
private void registerBackground() {
new AsyncTask() {
#Override
protected String doInBackground(Void... params) {
String msg = "";
try {
regid = gcm.register(GCM_SENDER_ID);
msg = "Device registered, registration id=" + regid;
// You should send the registration ID to your server over HTTP,
// so it can use GCM/HTTP or CCS to send messages to your app.
// For this demo: we don't need to send it because the device
// will send upstream messages to a server that will echo back
// the message using the 'from' address in the message.
// Save the regid for future use - no need to register again.
SharedPreferences.Editor editor = prefs.edit();
editor.putString(PROPERTY_REG_ID, regid);
editor.commit();
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
// Once registration is done, display the registration status
// string in the Activity's UI.
#Override
protected void onPostExecute(String msg) {
mDisplay.append(msg + "\n");
}
}.execute(null, null, null);
}
I get the error in compiling stating "Asynctask is a raw type. Reference to generic type should be paramterised.
You have not declared generic type parameters.
change
new AsyncTask() {
to
new AsyncTask<Void,Void,String>() {
and Also,
execute(null, null, null);
can be changed to
execute();

Not catching XMPPException when sent message using asmack

I am working in an android application to implement facebook chat and I have implemented it successfully using asmack libary. But when I tried to sent message when there is no internet connection XMPPException is not caught correctly. It shows Class file editor-source not found. I have downloaded my asmack libary from this link. Please look into my code and suggest me a solution.
Thanks.
public Boolean sentMessage(String message, Long senderid) {
ChatManager chatmanager = connection.getChatManager();
Chat newChat = chatmanager.createChat("-" + senderid
+ "#chat.facebook.com", new MessageListener() {
#Override
public void processMessage(Chat chat, Message message) {
System.out.println("Received message: " + message.getBody());
}
});
try {
newChat.sendMessage(message);
} catch (XMPPException e) {
return false;
}
return true;
}

Categories

Resources