java-firebase-fcm-client is push notification client for firebase cloud messaging API.
It is licensed under MIT license.
package com.example;
import org.riversun.fcm.FcmClient;
import org.riversun.fcm.model.EntityMessage;
import org.riversun.fcm.model.FcmResponse;
public class SendMessageExample1 {
public static void main(String[] args) {
FcmClient client = new FcmClient();
// You can get from firebase console.
// "select your project>project settings>cloud messaging"
client.setAPIKey("[YOUR_SERVER_API_KEY_HERE]");
// Data model for sending messages to specific entity(mobile devices,browser front-end apps)s
EntityMessage msg = new EntityMessage();
// Set registration token that can be retrieved
// from Android entity(mobile devices,browser front-end apps) when calling
// FirebaseInstanceId.getInstance().getToken();
msg.addRegistrationToken("REGISTRATION_TOKEN_FOR_AN_ANDROID_DEVICE_HERE");
// Add key value pair into payload
msg.putStringData("myKey1", "myValue1");
msg.putStringData("myKey2", "myValue2");
// push
FcmResponse res = client.pushToEntities(msg);
System.out.println(res);
}
}
{ "data":{
"myKey1":"myValue1",
"myKey2":"myValue2"
},
"registration_ids":["your_registration_token1","your_registration_token2]
}
Whether the application is foreground or background, you can handle push notifications in the same way.
https://github.com/riversun/android-firebase-fcm-client
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Map;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
String value1 = data.get("myKey1");
String value2 = data.get("myKey2");
}
}
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
//registration token
final String registrationToken = FirebaseInstanceId.getInstance().getToken();
//TODO
//Send to the server to register and identify the entity(mobile devices,browser front-end apps) in order to send a push notification to this entity(mobile devices,browser front-end apps).
}
}
<manifest>
<application>
...
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
...
</appliation>
</manifest>
Add this into dependencies
compile 'com.google.firebase:firebase-core:10.0.1'
compile 'com.google.firebase:firebase-messaging:10.0.1'
Add this, it is important to place this at the bottom line.
apply plugin: 'com.google.gms.google-services'
Add this into dependencies
classpath 'com.google.gms:google-services:3.0.0'
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (resultCode == ConnectionResult.SUCCESS) {
} else {
apiAvailability.getErrorDialog(this, resultCode, 1).show();
}
}
https://console.firebase.google.com/
https://firebase.google.com/docs/cloud-messaging/http-server-ref https://firebase.google.com/docs/cloud-messaging/send-message
https://github.com/riversun/android-firebase-fcm-client
<dependency>
<groupId>org.riversun</groupId>
<artifactId>fcm</artifactId>
<version>0.2.0</version>
</dependency>