Send a message to your Android device using Google's FCM service. Send messages using a server API that supports HTTP v1 so that you can receive push notifications on your Android device.
Enter the project name (any name) Analytics and billing regions: Japan Press Create Project
keytool -exportcert -list -v \
-alias androiddebugkey -keystore ~/.android/debug.keystore
You will be prompted to enter the password.
android
I was able to issue it at.
Paste the SHA-1 key output from the console when debugging.keystore is issued to the debug signature certificate SHA-1 (optional).
Press Register App.
Download google-services.json and place it directly under the Android Studio app.
Added the following services to AndroidManifest.xml.
<service
android:name="com.example.shosakaguchi.notifytest.FcmMessagingService"
>
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name="com.example.shosakaguchi.notifytest.FcmInstanceIdService"
>
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
Added the following dependencies to the app build.gradle.
api 'com.google.firebase:firebase-core:+'
api 'com.google.firebase:firebase-messaging:+'
api 'com.fasterxml.jackson.core:jackson-databind:+'
api 'com.google.firebase:firebase-auth:+'
Add the following dependencies to the project build.gradle + apply google-services.
dependencies {
api "com.google.firebase:firebase-messaging:10.2.4"
}
apply plugin: 'com.google.gms.google-services'
Rebuild the project.
Generate the following class in Android Studio.
A class that inherits FirebaseMessagingService (the contents are empty)
Arbitrary class name extends FirebaseMessagingService
Class inheriting FirebaseInstanceIdService
Arbitrary class name extends FirebaseInstanceIdService
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
Added code to get token and terminal token in MainActivity.java.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get device token
FcmInstanceIdService sv = new FcmInstanceIdService();
sv.onTokenRefresh();
}
}
Run the project on the device that receives the push notification.
Make a note of the token that appears in the Logcat console on Android Studio.
Create a new Spring Project using STS on Eclipse.
Add the following dependency to build.gradle.
compile('com.fasterxml.jackson.core:jackson-databind:+')
compile('com.google.firebase:firebase-admin:+')
From the Gradle task, hit buildDependents.
Prepare the following information from the settings page of the Firabase console page. -POST destination endpoint URL https://fcm.googleapis.com/v1/projects/全般ページのプロジェクトID/messages:send"
・ Private key Click on the service account from the Firabase console settings page Press "Generate New Private Key" on the Firebase Admin SDK tab. Honyara.json will be downloaded, so place it in the appropriate resource folder of your Spring project.
public static void initializeToken() throws IOException{
FileInputStream serviceAccount = new FileInputStream("Downloaded private key path");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://Project ID.firebaseio.com")
.build();
FirebaseApp.initializeApp(options);
}
public static String getAccessToken() throws IOException {
GoogleCredential googleCredential = GoogleCredential
.fromStream(new FileInputStream("Downloaded private key path"))
.createScoped(Arrays.asList("https://www.googleapis.com/auth/firebase.messaging"
));
googleCredential.refreshToken();
return googleCredential.getAccessToken();
}
For the time being, in order to send a message when the server application is executed, the main class should be processed.
TestProject.java
public static void main(String[] args) {
SpringApplication.run(PushAppTestApplication.class, args);
System.out.println("start");
try {
//initialize firebasio
initializeToken();
//post param create
URL url = new URL(Endpoint URL of the POST destination);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Authorization", "Bearer " + getAccessToken());
// System.out.println("token" + getAccessToken());
httpURLConnection.setRequestProperty("Content-Type", "application/json; UTF-8");
httpURLConnection.setDoOutput(true);
httpURLConnection.connect();
OutputStream out = httpURLConnection.getOutputStream();
PrintStream ps = new PrintStream(out);
ps.print(BODY part of request message JSON (described later));
ps.close();
//return response output
InputStream is = httpURLConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String s;
while ((s = reader.readLine()) != null) {
System.out.println(httpURLConnection.getResponseMessage() + httpURLConnection.getResponseCode());
}
reader.close();
httpURLConnection.disconnect();
} catch(Exception e) {
e.printStackTrace();
}
System.out.println("end");
}
BODY part of request message JSON
private final static String reqJson = "{\n" +
" \"message\":{\n" +
" \"token\" : \"Tokens appearing in the Logcat console on Android Studio",\n" +
" \"notification\" : {\n" +
" \"body\" : \"Test message\",\n" +
" \"title\" : \"Test title\",\n" +
" }\n" +
" }\n" +
"}";
That's all for implementation.
This time, I mainly implemented sending push notifications to a single terminal. In practice, it is necessary to think carefully about the assumption when sending to multiple terminals, the assumption of transmission time (cron batch etc.), the mechanism to send the device token to the server and storing it.
From the next time, I would like to introduce those HowTos and examples.
Thank you for your hard work.
Recommended Posts