我正在为Android Wear-OS开发实时聊天应用程序。我正在使用Tic Watch Pro 4G进行开发测试。我的应用程序已经在Wear-OS Play商店中,名称为“ IoSite”
对于推送通知,我正在使用FCM。我有两块手表
案例1:如果我使用Android手机配置手表,并从Play商店下载IoSite App,则FCM Push通知在这两个手表中均有效。
案例2:如果我使用iPhone配置手表。然后,当我将IoSite应用程序下载到手表1中并尝试使用它时。然后FCM推送通知可以正常工作。然后,我在手表2中下载了IoSite应用并尝试使用它。 FCM推送通知可以很好地在两个手表中使用。但是在将应用安装在手表2中后,FCM推送通知将在手表1中停止工作。以下是该应用尝试获取Firebase实例ID时的Android Studio日志:
2020-02-14 16:39:39.455 9476-9662 / com.iosite.iositewatchappW / FirebaseInstanceId:令牌检索无一例外失败信息。将重试令牌检索2020-02-14 16:39:39.4599476-9476 / com.iosite.iositewatchapp W / MyFirebaseMessagingService:getInstanceId失败java.io.IOException:SERVICE_NOT_AVAILABLEcom.google.firebase.iid.zzl.zza(com.google.firebase:firebase-iid @@ 20.0.2:71)com.google.firebase.iid.zzl.zza(com.google.firebase:firebase-iid @@ 20.0.2:84)com.google.firebase.iid.zzp.then(com.google.firebase:firebase-iid @@ 20.0.2:4)在com.google.android.gms.tasks.zzd.run(未知来源:5)在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:641)在java.lang.Thread.run(Thread.java:764)
在此手表1中,如果我尝试删除InstanceID(以便由FCM SDK生成新的ID),则出现以下错误:
2020-02-14 16:39:39.461 9476-9476 / com.iosite.iositewatchappW / System.err:位于com.google.firebase.iid.FirebaseInstanceId.deleteInstanceId(com.google.firebase:firebase-iid @@ 20.0.2:57)2020-02-14 16:39:39.462 9476-9476 / com.iosite.iositewatchappW / System.err:位于com.iosite.iositewatchapp.Services.MyFirebaseMessagingService $ 2.onComplete(MyFirebaseMessagingService.java:407)
请提出解决方案。
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private final String TAG = "MyFirebaseMessaging";
private Context mContext;
public static final String CHANNEL_ID = "NotifChannel";
public static final String CHANNEL_ID_ANN = "AnnouncementChannel";
public static final String CHANNEL_ID_MSG = "MsgChannel";
private String fcm_token = "";
public static List<MessageClass> messages = new ArrayList<>();
Location location;
private Vibrator audioVibe;
private VibrationEffect audioVibeEffect;
public MyFirebaseMessagingService() {
}
@Override
public void onCreate() {
super.onCreate();
}
public MyFirebaseMessagingService(Context context) {
Log.i(TAG, "MyFirebaseMessagingService started");
mContext = context;
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "onMessageReceived called: " + remoteMessage.getData());
Map<String, String> messageReceivedData = remoteMessage.getData();
if (messageReceivedData.size() > 0) {
//TODO: handle new group added notif and subscribe for the group. Also add group to group list
// sendMessageNotification(messageReceivedData);
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}
@Override
public void onNewToken(String token) {
Log.d(TAG, "New token generated: " + token);
fcm_token = token;
// sendRegistrationToServer(token, FirebaseInstanceId.getInstance().getId());
}
public boolean isGooglePlayServicesAvailable(Context context) {
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(context);
return resultCode == ConnectionResult.SUCCESS;
}
此外,将以下内容添加到您的应用程序级别build.gradle:
implementation 'com.google.firebase:firebase-messaging:20.1.2'
将以下内容添加到清单中:
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
在上面的代码之后,您还需要将项目添加到fcm帐户。添加项目后,您将获得google-services.json文件。将该文件保存在C:\Users\{xyz}\AndroidStudioProjects\{your_app_name}\app\
您应该能够通过回调方法onMessageReceived
接收所有通知。