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> </intent-filter>
</receiver> </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"> <service android:name=".firebase.FirebaseMessaging">
<intent-filter> <intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" /> <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.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.Toast; 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.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -750,21 +755,26 @@ public class LinphoneManager implements CoreListener, SensorEventListener, Accou
private void initPushNotificationsService() { private void initPushNotificationsService() {
if (getString(R.string.push_type).equals("firebase")) { if (getString(R.string.push_type).equals("firebase")) {
try { try {
Class<?> firebaseClass = FirebaseInstanceId.getInstance()
Class.forName("com.google.firebase.iid.FirebaseInstanceId"); .getInstanceId()
Object firebaseInstance = firebaseClass.getMethod("getInstance").invoke(null); .addOnCompleteListener(
final String refreshedToken = new OnCompleteListener<InstanceIdResult>() {
(String) firebaseClass.getMethod("getToken").invoke(firebaseInstance); @Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
// final String refreshedToken = if (!task.isSuccessful()) {
// com.google.firebase.iid.FirebaseInstanceId.getInstance().getToken(); Log.w(
if (refreshedToken != null) { "[Push Notification] firebase getInstanceId failed: "
Log.i( + task.getException());
"[Push Notification] init push notif service token is: " return;
+ refreshedToken); }
LinphonePreferences.instance() String token = task.getResult().getToken();
.setPushNotificationRegistrationID(refreshedToken); Log.i(
} "[Push Notification] init push notif service token is: "
+ token);
LinphonePreferences.instance()
.setPushNotificationRegistrationID(token);
}
});
} catch (Exception e) { } catch (Exception e) {
Log.i("[Push Notification] firebase not available."); 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 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 This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License 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.LinphoneManager;
import org.linphone.LinphoneService; import org.linphone.LinphoneService;
import org.linphone.mediastream.Log; import org.linphone.mediastream.Log;
import org.linphone.settings.LinphonePreferences;
import org.linphone.utils.LinphoneUtils; import org.linphone.utils.LinphoneUtils;
public class FirebaseMessaging extends FirebaseMessagingService { public class FirebaseMessaging extends FirebaseMessagingService {
public FirebaseMessaging() {} 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 @Override
public void onMessageReceived(RemoteMessage remoteMessage) { public void onMessageReceived(RemoteMessage remoteMessage) {
android.util.Log.i("FirebaseMessaging", "[Push Notification] Received"); android.util.Log.i("FirebaseMessaging", "[Push Notification] Received");