Using new methods of Firebase push messaging

This commit is contained in:
Sylvain Berfini 2019-02-19 10:40:11 +01:00
parent 910ccd69c5
commit 85fa30623d
4 changed files with 40 additions and 67 deletions

View file

@ -271,13 +271,6 @@
</intent-filter>
</receiver>
<service
android:name=".firebase.FirebaseIdService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".firebase.FirebaseMessaging">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />

View file

@ -56,6 +56,11 @@ import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@ -750,21 +755,26 @@ public class LinphoneManager implements CoreListener, SensorEventListener, Accou
private void initPushNotificationsService() {
if (getString(R.string.push_type).equals("firebase")) {
try {
Class<?> firebaseClass =
Class.forName("com.google.firebase.iid.FirebaseInstanceId");
Object firebaseInstance = firebaseClass.getMethod("getInstance").invoke(null);
final String refreshedToken =
(String) firebaseClass.getMethod("getToken").invoke(firebaseInstance);
// final String refreshedToken =
// com.google.firebase.iid.FirebaseInstanceId.getInstance().getToken();
if (refreshedToken != null) {
FirebaseInstanceId.getInstance()
.getInstanceId()
.addOnCompleteListener(
new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(
"[Push Notification] firebase getInstanceId failed: "
+ task.getException());
return;
}
String token = task.getResult().getToken();
Log.i(
"[Push Notification] init push notif service token is: "
+ refreshedToken);
+ token);
LinphonePreferences.instance()
.setPushNotificationRegistrationID(refreshedToken);
.setPushNotificationRegistrationID(token);
}
});
} catch (Exception e) {
Log.i("[Push Notification] firebase not available.");
}

View file

@ -1,44 +0,0 @@
/*
FirebaseIdService.java
Copyright (C) 2017 Belledonne Communications, Grenoble, France
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.linphone.firebase;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
import org.linphone.settings.LinphonePreferences;
import org.linphone.utils.LinphoneUtils;
public class FirebaseIdService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
final String refreshedToken = FirebaseInstanceId.getInstance().getToken();
android.util.Log.i(
"FirebaseIdService", "[Push Notification] Refreshed token: " + refreshedToken);
LinphoneUtils.dispatchOnUIThread(
new Runnable() {
@Override
public void run() {
LinphonePreferences.instance()
.setPushNotificationRegistrationID(refreshedToken);
}
});
}
}

View file

@ -2,7 +2,7 @@ package org.linphone.firebase;
/*
FirebaseMessaging.java
Copyright (C) 2017 Belledonne Communications, Grenoble, France
Copyright (C) 2017-2019 Belledonne Communications, Grenoble, France
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
@ -27,11 +27,25 @@ import com.google.firebase.messaging.RemoteMessage;
import org.linphone.LinphoneManager;
import org.linphone.LinphoneService;
import org.linphone.mediastream.Log;
import org.linphone.settings.LinphonePreferences;
import org.linphone.utils.LinphoneUtils;
public class FirebaseMessaging extends FirebaseMessagingService {
public FirebaseMessaging() {}
@Override
public void onNewToken(final String token) {
android.util.Log.i("FirebaseIdService", "[Push Notification] Refreshed token: " + token);
LinphoneUtils.dispatchOnUIThread(
new Runnable() {
@Override
public void run() {
LinphonePreferences.instance().setPushNotificationRegistrationID(token);
}
});
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
android.util.Log.i("FirebaseMessaging", "[Push Notification] Received");