Skip to content

[Android] Add FCM quickstart example. #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Chat-Android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ buildscript {
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.2"

// Firebase
classpath 'com.google.gms:google-services:4.3.15'
// Add the Crashlytics Gradle plugin (be sure to add version
// 2.0.0 or later if you built your app with Android Studio 4.1).
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
1 change: 1 addition & 0 deletions Chat-Android/fcmquickstart/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
48 changes: 48 additions & 0 deletions Chat-Android/fcmquickstart/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}

android {
compileSdkVersion 33

defaultConfig {
applicationId "io.agora.fcmquickstart"
minSdkVersion 21
targetSdkVersion 33
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
// Google firebase cloud messaging
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:32.2.3')
// Declare the dependencies for the Firebase Cloud Messaging and Analytics libraries
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.firebase:firebase-analytics'
// Required: Agora Chat SDK
implementation 'io.agora.rtc:chat-sdk:1.1.0'
}
68 changes: 68 additions & 0 deletions Chat-Android/fcmquickstart/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"project_info": {
"project_number": "142290967082",
"project_id": "chatim-9b3ae",
"storage_bucket": "chatim-9b3ae.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:142290967082:android:fa1734c82786f78184c153",
"android_client_info": {
"package_name": "io.agora.chatdemo"
}
},
"oauth_client": [
{
"client_id": "142290967082-kffai4lca6v8f70lbku18hquh72747qa.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyDE682uNgGqIfEL7o6BkShzx5Pi2nuIIS0"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "142290967082-kffai4lca6v8f70lbku18hquh72747qa.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
},
{
"client_info": {
"mobilesdk_app_id": "1:142290967082:android:096ac679236ff8d084c153",
"android_client_info": {
"package_name": "io.agora.fcmquickstart"
}
},
"oauth_client": [
{
"client_id": "142290967082-kffai4lca6v8f70lbku18hquh72747qa.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyDE682uNgGqIfEL7o6BkShzx5Pi2nuIIS0"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "142290967082-kffai4lca6v8f70lbku18hquh72747qa.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
}
],
"configuration_version": "1"
}
21 changes: 21 additions & 0 deletions Chat-Android/fcmquickstart/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
34 changes: 34 additions & 0 deletions Chat-Android/fcmquickstart/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.agora.fcmquickstart">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ChatAndroid">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- FCM required start -->
<service android:name=".FCMMSGService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package io.agora.fcmquickstart;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import io.agora.chat.ChatClient;
import io.agora.util.EMLog;

public class FCMMSGService extends FirebaseMessagingService {
private static final String CHANNEL_ID = "fcmquickstart_notification";
private static final long[] VIBRATION_PATTERN = new long[]{0, 180, 80, 120};
private static int NOTIFY_ID = 1000; // start notification id
private static final String TAG = "EMFCMMSGService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().size() > 0) {
String message = remoteMessage.getData().get("alert");
EMLog.i(TAG, "onMessageReceived: " + message);
notify(message);
}
}

@Override
public void onNewToken(@NonNull String token) {
super.onNewToken(token);
EMLog.i(TAG, "onNewToken: " + token);
ChatClient.getInstance().sendFCMTokenToServer(token);
}

private void notify(String message) {
NotificationManager notificationManager = (NotificationManager) getApplication().getSystemService(Context.NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= 26) {
// Create the notification channel for Android 8.0
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
"fcm quick start message default channel.", NotificationManager.IMPORTANCE_DEFAULT);
channel.setVibrationPattern(VIBRATION_PATTERN);
notificationManager.createNotificationChannel(channel);
}
try {
NotificationCompat.Builder builder = generateBaseBuilder(message);
Notification notification = builder.build();
notificationManager.notify(NOTIFY_ID, notification);

} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Generate a base Notification#Builder, contains:
* 1.Use the app icon as default icon
* 2.Use the app name as default title
* 3.This notification would be sent immediately
* 4.Can be cancelled by user
* 5.Would launch the default activity when be clicked
*
* @return
*/
private NotificationCompat.Builder generateBaseBuilder(String content) {
PackageManager pm = getApplication().getPackageManager();
String title = pm.getApplicationLabel(getApplication().getApplicationInfo()).toString();
Intent i = getApplication().getPackageManager().getLaunchIntentForPackage(getApplication().getApplicationInfo().packageName);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplication(), NOTIFY_ID, i, PendingIntent.FLAG_UPDATE_CURRENT);

return new NotificationCompat.Builder(getApplication(), CHANNEL_ID)
.setSmallIcon(getApplication().getApplicationInfo().icon)
.setContentTitle(title)
.setTicker(content)
.setContentText(content)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentIntent(pendingIntent);
}
}
Loading