update single row item in listview android - android

i am developing android chat application like whatsapp/hangout.
what i am doing is, Once message is sent to server, the status for message is "sent". And when i receive acknowledgement for that message, i want to change the status for that message to "Delivered".
How to do that ??
Code before sending to server
ChatMessageModel chatMessageModel = new ChatMessageModel();
chatMessageModel.setId(nextId);
chatMessageModel.set_id(null);
chatMessageModel.setGroupId(groupID);
chatMessageModel.setMessage(message);
chatMessageModel.setSenderUserId(user.getUserId());
chatMessageModel.setSenderDisplayName("");
chatMessageModel.setDateSent(null);
chatMessageModel.setMessageType("message");
chatMessageModel.setStatus("sent");
chatMessageService.saveIndMessage(chatMessageModel);
mChatMessageCustomAdapter.add(chatMessageModel);
mChatMessageCustomAdapter.notifyDataSetChanged();
After receiving ACK from server, code is
chatMessageModel.set_id(msg.getString("_id"));
chatMessageModel.setGroupId(msg.getString("groupId"));
chatMessageModel.setMessage(msg.getString("message"));
chatMessageModel.setSenderUserId(msg.getString("senderUserId"));
chatMessageModel.setSenderDisplayName(msg.getString("senderDisplayName"));
chatMessageModel.setStatus("Delivered");
****here how do i update status to "delivered"***

Use update method instead of add inside your adapter and update at particular position and then use notifyitemchanged().

Related

Cancel notification recieved from FireBase client side Android/IOS?

Is it possible to cancel a pushed notification before displaying it on the users phone ?
I have some custom logic which decides whether a notification needs to be displayed/appear . Is it possible for me to control this behaviour from the client side ios/android code ?
Once a message is sent to Firebase Cloud Message, there is no way to cancel its delivery.
If your message contains only a data element and no notification, displaying the message is handled by your application code - so in that case you may be able to suppress its display there.
Although the best way is to handle this is to cancel it on backend side, you still can add UNNotificationServiceExtension and override the didReceive method:
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: #escaping (UNNotificationContent) -> Void) {
self.receivedRequest = request;
self.contentHandler = contentHandler
self.content = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let content = self.content {
// I had to check something inside the push itself
if let infoDictionary = content.userInfo {
// Check something inside the push notification
contentHandler(content)
return
}
}
// Otherwise, send an empty notification to the system and it will show nothing
contentHandler(UNNotificationContent())
}

How to send a notification based on spinner value on android app?

I'm doing my final year project about flood push notification. Previously, a user will retrieve notifications whenever the value in database changes from 0 to 1. But right now,in my app, the user can choose which places he parks his car and he can retrieved status and notification based on the park's place whether it's zero or one. As for example, User A set his spinner value in Mid Valley and User B set his spinner value in KPS. User A will get notification when FLOOD_STATUS_MID_VALLEY is 1, while User B didn't receive any notifications because FLOOD_STATUS_KPS is still 0. Anyone can help me on how to send notification based on the spinner value in the app?
Screenshot of my app
Screenshot
below is the python code that running on server to send notification based on a value called "FLOOD_STATUS" in firebase database
from pusher_push_notifications import PushNotifications
config = {
'apiKey': "APIKEY",
'authDomain': "car-app-push-notification.firebaseapp.com",
'databaseURL': "https://car-app-push-notification.firebaseio.com",
'projectId': "car-app-push-notification",
'storageBucket': "car-app-push-notification.appspot.com",
'messagingSenderId': "596729642105",
'appId': "APPID",
'measurementId': "G-9LMJGS1BDW"
}
firebase = pyrebase.initialize_app(config)
db = firebase.database()
beams_client = PushNotifications(
instance_id='49f62b05-bd81-4040-ab57-80afa56a8680',
secret_key='SECRET KEY',
)
def stream_handler(message):
print(message)
if(message['data'] is 1):
response = beams_client.publish_to_interests(
interests=['hello'],
publish_body={
'apns': {
'aps': {
'alert': 'Hello!',
},
},
'fcm': {
'notification': {
'title': 'Alert!',
'body': 'It is starting to flood, please remove your car immediately!',
},
},
},
)
print(response['publishId'])
my_stream = db.child("FLOOD_STATUS").stream(stream_handler,None)
As this is your FYP, I won't provide any code for this.
When a user changes where they parked, you will want to change their device interest to match the relevant location.
When the FLOOD_STATUS is updated to 1 for a given location, you send the notification to the matching interest.
I suggest that, when the user A select the MID_VALLEY in the Spinner you send the selected value to the server. I recommend you to have a look on this tutorial to see how to get the selected value from Spinner
At server side, when the FLOOD_STATUS_MID_VALLEY is 1 you only send the notification to the user A(because you knew the FCM token of the user A).
If you want to custom the item of Spinner you can watch this tutorial.

Smack Push Notification not working

I am using Smack library to get XMPP pubsub requests in my Android App.
I want to send alert from 1 phone which is received on another phone as push notification popup.
On debugging, control returns null due to this code in Smack 4.2.0 library's ChatManager.java class.
EntityJid userJID = from.asEntityJidIfPossible();
if (userJID == null) {
LOGGER.warning("Message from JID without localpart: '" + message.toXML() + "'");
return null;
}
My from isnt null,ofc... but, my asEntityjidIfPossibe is null.
My Iq is as follows:
<iq
to="pubsub.foo"
from="notifier#foo"
type="set"
id="1523272621323">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<publish xmlns="http://jabber.org/protocol/pubsub"
node="/home/foo/notifier/c7215880-a36e-4cb9-bdd3-441cf9c29c31/pqr">
<item id="item11523272621323">
<json xmlns="urn:xmpp:json:0">
{"id":"b7da305d-77c2-422a-9c95-7ec8a347192e",
"orgId":"c7215880-a36e-4cb9-bdd3-441cf9c29c31",
"user_id":"f0622ed6-860f-4f4e-9b11-c4ab6cb8cd66",
"username":"xyz",
"subject":"Message",
"body":"hello all",
"priority":"NORMAL",
"ack_required":false,
"users":["pqr"],
"replyTo":["/home/foo/notifier/c7215880-a36e-4cb9-bdd3-441cf9c29c31/xyz",
"/home/foo/notifier"]}
</json>
</item>
</publish>
</pubsub>
</iq>
All I want is to see if message sent is recieved or not? And how to get data to process it...Data is sent perfectly on the server from the sending phone.
On recieving device,
I get this message TWICE,
Message from JID without localpart: '<message to='xyz#foo/5301412210663914056363834' from='pubsub.foo'><event xmlns='http://jabber.org/protocol/pubsub#event'><items node='/home/foo/notifier/c7215880-a36e-4cb9-bdd3-441cf9c29c31/xyz'><retract id='item11523351450156'/></items></event></message>'

Cross platform (iOS-Android) xmpp request

I am creating one chatting application which will work on both iOS and Android platform. Sometimes 'both' subscription is not received at both end. Can anyone tell me what can be the possible issue?
===================== For iOS =====================
Sending request,
XMPPJID *XMPPJIDObj=[XMPPJID jidWithString:aStrOtherJabberId];
[appDelegateObj.xmppRoster addUser:XMPPJIDObj withNickname:nil];
Accepting request,
[appDelegateObj.xmppRoster acceptPresenceSubscriptionRequestFrom:aReceiverJID andAddToRoster:TRUE];
Removing user,
[appDelegateObj.xmppRoster removeUser:[XMPPJID jidWithString:aPresenceObj.userJabberID]];
===================== For Android =====================
Sending request,
Roster.setDefaultSubscriptionMode(SubscriptionMode.manual);
myApp.getXmppConnection().getRoster().createEntry(visitorJabberId, visitorUserName, null);
Accepting request,
final Presence presence1 = new Presence(Type.subscribed);
presence1.setFrom(myApp.getUserJabberId());
presence1.setType(Type.subscribed);
presence1.setTo(visitorJabberId);
myApp.getXmppConnection().sendPacket(presence1);
myApp.getXmppConnection().getRoster().createEntry(visitorJabberId, visitorUserName, null);
Removing user,
final RosterPacket rosterPacket = new RosterPacket();
rosterPacket.setType(IQ.Type.SET);
final RosterPacket.Item item = new RosterPacket.Item(visitorJabberId, null);
item.setItemType(RosterPacket.ItemType.remove);
rosterPacket.addRosterItem(item);
myApp.getXmppConnection().sendPacket(rosterPacket);
Hi Leena for the iOS we have used save thing but I think you have forgot some thing. The actual flow is call add user method of roster class then call subscribe method with subscription value YES and finally send presence tag with subscribe type to the serve. Following are the code here I used XMPPSharedPreference singleton class rather than appdelegate. Hope this will work for you...
XMPPJID *newBuddy = [XMPPJID jidWithString:JIDString];
[[XMPPSharedPreference sharedPreferences].xmppRoster addUser:newBuddy withNickname:#"nicknameValue"];
[[XMPPSharedPreference sharedPreferences].xmppRoster acceptPresenceSubscriptionRequestFrom:newBuddy andAddToRoster:YES];
NSXMLElement *presence = [NSXMLElement elementWithName:#"presence"];
[presence addAttributeWithName:#"to" stringValue:JIDString];
[presence addAttributeWithName:#"type" stringValue:#"subscribe"];
[[self xmppStream] sendElement:presence];
When you add a user to your roster you have to make sure you also subscribe to the friend's presence. That completes the cycle.
So, for iOS for example, you're adding a friend to roster like this:
[appDelegateObj.xmppRoster addUser:XMPPJIDObj withNickname:nil];
But you need to do use this instead:
- (void)addUser:(XMPPJID *)jid withNickname:(NSString *)optionalName groups:(NSArray *)groups subscribeToPresence:(BOOL)subscribe
and make sure you set subscribe to YES
Or, you could keep the code you have but manually subscribe to the user's presence by doing this:
[appDelegateObj.xmppRoster subscribePresenceToUser:XMPPJIDObj]
Let me know how that works out for you.

Asmack android: Create a custom packet and register extension

I am using the library Asmack to use XMPP on an android app, the server sends custom Stanza
<notification xmlns="jabber:client" from="admin#api.pp.gs" to="1eef368606cb459b847809a0430bfa1f#api.pp.gs/iMac-de-Thomas" id="1457247499">
<body>lol</body>
</notification>
I would like to be able to listen these packets, I try to use PacketExtensionProvider but without success. Here my code to listen the packet:
xmppConnection.addPacketListener(new PacketListener()
{
#Override
public void processPacket(Packet packet)
{
Log.i("K:", "cool " + packet.getClass().toString());
}
}, new MyPaquetFilter());
Where MyPaquetFilter return always true for test purpose.
How should I use the PacketManager, PacketExtension stuff in order to get my custom paquet here?
In order to be able to receive a custom packet you need to first create a Provider, either an IQProvider or an PacketExtensionProvider, and register that Provider with ProviderManager, so that's considered for parsing when receiving a packet.
You didn't showed us your custom PacketExtensionProvider nor how you are registering it with the ProviderManager. And just to absolute precise, you need to wrap your custom extension notification within the message element if you use PacketExtensionProvider.

Categories

Resources