Ad

Flutter: How To SubscribeToTopic For Firebase (FCM) Registration Token In Flutter

I want to subscribeToTopic for Firebase push notification in flutter , I can subscribe to any topic but my purpose to subscribeToTopic against token , here it is the code below,

  FirebaseMessaging _messaging;
 Firebase.initializeApp().then((fbr) {
      _messaging = FirebaseMessaging.instance;
      _messaging.getToken().then((token) async{

      _messaging.subscribeToTopic(token).then((value) => null);

      _messaging.subscribeToTopic(userID).then((value) => null);
      _messaging.subscribeToTopic(userTypeId).then((value) => null);
}

}

But I am getting error

Failed assertion: line 307 pos 10: 'isValidTopic': is not true.

log attached below:

I/flutter ( 9352): Token: dhCABsvbQ8udQJha8VOxNy:APA91bEGcpS6-QMn-c236ITNbDtxEs3MD1Q-nquedMLzZv4XWPdGUWt-Zw-OQ6YBY383IJGZXxKXNMRJd8SKLeOO7agx4dcym6VoEhPTrbYr20NoscZHTZoPCf5mqbfiTHCS5q2WlXqw
I/flutter ( 9352): User granted permission
I/flutter ( 9352): Token: dhCABsvbQ8udQJha8VOxNy:APA91bEGcpS6-QMn-c236ITNbDtxEs3MD1Q-nquedMLzZv4XWPdGUWt-Zw-OQ6YBY383IJGZXxKXNMRJd8SKLeOO7agx4dcym6VoEhPTrbYr20NoscZHTZoPCf5mqbfiTHCS5q2WlXqw
E/flutter ( 9352): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: 'package:firebase_messaging/src/messaging.dart': Failed assertion: line 307 pos 10: 'isValidTopic': is not true.
E/flutter ( 9352): #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)
E/flutter ( 9352): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
E/flutter ( 9352): #2      _assertTopicName (package:firebase_messaging/src/messaging.dart:307:10)
E/flutter ( 9352): #3      FirebaseMessaging.subscribeToTopic (package:firebase_messaging/src/messaging.dart:294:5)
E/flutter ( 9352): #4      _HomePageState.registerNotification.<anonymous closure> (package:notify/main.dart:89:20)
E/flutter ( 9352): #5      _rootRunUnary (dart:async/zone.dart:1362:47)
E/flutter ( 9352): #6      _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter ( 9352): #7      _FutureListener.handleValue (dart:async/future_impl.dart:152:18)
E/flutter ( 9352): #8      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:704:45)
E/flutter ( 9352): #9      Future._propagateToListeners (dart:async/future_impl.dart:733:32)
E/flutter ( 9352): #10     Future._completeWithValue (dart:async/future_impl.dart:539:5)
E/flutter ( 9352): #11     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:254:13)
E/flutter ( 9352): #12     MethodChannelFirebaseMessaging.getToken (package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart)
E/flutter ( 9352): <asynchronous suspension>
Ad

Answer

The error is coming up because the token id you're passing as the topic name is not an acceptable topic name.

Here is the check that the token fails:

// https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/firebase_messaging/lib/src/messaging.dart

void _assertTopicName(String topic) {
    bool isValidTopic = RegExp(r'^[a-zA-Z0-9-_.~%]{1,900}$').hasMatch(topic);
    assert(isValidTopic);
}

Source

Use a valid string and the code should work fine.

Ad
source: stackoverflow.com
Ad