Android send notification from device to other device using GCM and PHP - java

I need a help of how to send a notification from device to other device using the same application by GCM and PHP as a server.
can anyone provide me a simple example of how to register as a user and send message to another user. or provide me anything that will be useful to my application

I think I know what you are talking about. First things first, if you want to send a Google Cloud Message from device A to to device B, you must have the GCM registration Id of device B. (I hope you are not planning on storing device B's registration Id on device A)
Here is one approach
Create a registration page. Take a unique input from user like email address (don't forget to verify it) and get GCM registration Id. Then store this information in server with the unique input (email) as primary key and map it with GCM registration Id.
Now if you want the USER of the app, say user A to send some information to user B (like chatting app) call your PHP method from device A with user B's unique input (email address). Your web method can fetch the GCM registration Id for device B from the input parameter and can send a GCM message. you can get email address of user B, C, D etc. from user's contact book or contacts in the email address (or use social networking SDKs to get email address).
If you want your app to send GCM message from device A to device B without users consent, then also you either need the registration Id or the mapped unique key of device B (email in this example). I don't really see any use in this case. But since you didn't mention any detail in your original question I am giving the possibilities.
you cannot use IMEI number as device A does not know the IMEI number of device B. And as mentioned earlier you cannot store all the registration Ids from USER A, B, C, D.... in device A.

Related

firebase auhentication both email and phone sms verification

In Firebase, I register the user with e-mail on the authentication side and after registration I send e-mail verification to this user, but in addition to this, I define some other features in my system, such as name, phone number while registering. I will combine these features and keep them in user collections in firestore. At the same time, I want to send an sms-code to the phone number of the data whose e-mail and password are matched in the user collections with the e-mail and password entered by the user while logging in to the system, and I will instantly verify this every time for the user to log in. Is it possible for me to do something like this or should I just choose one over email or phone?
Yes absolutely it is possible, use both authentication method one by one (email first then phone OTP) and while registering after creating user with email and password and sending otp to the user, instead of doing createUserWithCredentials use linkUserWithCredentials. This will create one user with both email and phone number authentication instead of creating two separate users for two different credentials.

Getting the List of registered devices from fcm for my site

I am new to FCM and mobile coding. This is what I am trying to achieve:-
Develop an app to allow users to select some events.
I have a site that will loop through the events for users. When an event is close to its start time or some messages were created for
that event, I will send a FCM message to all the devices that
registered to that event.
I am confused on the implementation. This is what I am thinking:
When my app starts, I can register for push notification and it will return a devicetoken.
When user saves an event I can pass the devicetoken back to the server to re remember it.
In my site's code, I have some code to detect if an event is close and sends notification base on the devicetoken linked to
that event.
Is this about the right way to code? But if the user restarts the app or restarts the phone, isn't that I will get a new devicetoken? So I need to store some other identifier to identify a user (e.g. google plus user name)?
Is this about the right way to code? But if the user restarts the app or restarts the phone, isn't that I will get a new devicetoken? So I need to store some other identifier to identify a user (e.g. google plus user name)?
Yes it is right, and check the below for the questions:
You need to use FirebaseInstanceIdService it is used to handle the creation, rotation, and updating of registration tokens.
To retrieve the token of a device use this:
FirebaseInstanceId.getInstance().getToken()
The above token that you get may change in the following situations:
Instance ID is stable except when:
App deletes Instance ID
App is restored on a new device
User uninstalls/reinstall the app
User clears app data
Then using this method inside FirebaseInstanceIdService, it will refresh the token whenever any one of the situation happens:
#Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
}
After you've obtained the token, you can send it to your app server and store it using your preferred method.
more info here: https://firebase.google.com/docs/cloud-messaging/android/client

Open and pick a contact from a device contact app

I am building an invitation form in which the user can fill an email or use a button that will open a contact app in his device and will get the email of a chosen contact(if email exists). I wonder if this kind of button is possible to achieve in Codename One without listing all the user's contacts in my app.
Thanks!
Itay
According to the official API, you may only use id to retrieve a certain contact using method getContactById(String id).
So, the answer is "No", you cannot get the contact by its email since you don't know the mapping id <> email

How to send a message to a specific user of an FCM (Firebase Cloud Messaging) token?

The issue is that FCM provides a token for each unique app for a device, so let's say two users use the same device and application, one signs out so that the other can sign in and use the app, i'm confused about how the flow should go! now the two users have the same token so both of them will receive the upcoming messages.
A similar question was asked here and here but it's still not clear for me!
Any help will be appreciated.
Generate a user specific unique code to each user by yourself at the time of login.send the user specific unique code along with the push notification from server end.
Now send push notifications to all users.and when notification receives check check the user specific unique code to identify the user
When the App is killed, then by default Android will shows the 'notification data' as Notification, So the 'notification data' should be common to all users.
User specific data should be added as 'data message'.When a push notification arrives, the default Notification will be shown.And the 'data message' will get through the Intent in the launcher activity.Here you can identify the user by the user specific unique code and respond to push notification.

Send post notification to specific UUID android device

I'm trying to find some ways to send a post notification to specific android device using its UUID , so I will do the following :
1 : login screen on android and then get UUID number from the device
2 : send this UUID to online database linked to userID
3 : I want to know how to send post notification to a specific UUID which I get from my database ?
Sending notification to android device using java is very easy.
If you want to push the notification directly from REST client, then you can refer this
If you want to send notifications from your java program, then follow these simple steps,
Add gcm-server.jar library file in your application
Create a Sender, that will send the notification
com.google.android.gcm.server.Sender sender = new Sender("pass your gcm api key here");
Then create a message object that will contain the data that you want to send
com.google.android.gcm.server.Message message = new Message.Builder().addData("key","value").build();
addData("key","value") in above code will add the key value in json to be send.
Then you can choose any of send method provided in Sender class
com.google.android.gcm.server.Result result = sender.send(message, device_token, no of retries);
There are total four version of send method available. You can find more detail about Sender class here

Categories