이후 아래와 같이 TalkPlus에서 제공하는 processFirebaseCloudMessagingData 함수를 호출하는 코드를 삽입하여 Push Notification을 처리할 수 있습니다.
Foreground인 경우 기존에 등록되어있는 ChannelListener를 통하여 자동으로 콜백을 올리게 됩니다.
Background인 경우 앱이 실행되어있지 않은 상태라 등록된 콜백이 없을 수 있으므로 아래와 같이 1회용 콜백을 함께 등록하여 이벤트를 받아 원하는 동작을 수행하실 수 있습니다.
processFirebaseCloudMessagingData 함수에서 마지막 인자값으로 forceCallback을 boolean 으로 받게 되어 있는데, forceCallback은 실시간 채널에서 콜백 수신과 상관없이 FCM 통해 무조건 콜백을 받아 처리하기 위해서는 true로 넘겨주셔야 합니다.
class Application extends StatefulWidget {
@override
State<StatefulWidget> createState() => _Application();
}
class _Application extends State<Application> {
// It is assumed that all messages contain a data field with the key 'type'
Future<void> setupInteractedMessage() async {
// Get any messages which caused the application to open from
// a terminated state.
RemoteMessage? initialMessage =
await FirebaseMessaging.instance.getInitialMessage();
// If the message also contains a data property with a "type" of "chat",
// navigate to a chat screen
if (initialMessage != null) {
_handleMessage(initialMessage);
}
// Also handle any interaction when the app is in the background via a
// Stream listener
FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
}
void _handleMessage(RemoteMessage message) {
if (message.data.containsKey("talkplus")) {
// channelId, title, body 정보가 필요한 경우 아래와 같이 사용합니다.
// var msg = convert.jsonDecode(message.data['talkplus'])
// String channelId = talkplus.getString("channelId");
// String messageId = talkplus.getString("messageId"); // available only for message event
// String title = talkplus.getString("title");
// String body = talkplus.getString("body");
await TalkPlusAPI.processFirebaseCloudMessagingData(message.data, onetimeListener, forceCallback);
}
}
@override
void initState() {
super.initState();
// Run code required to handle interacted messages in an async function
// as initState() must not be async
setupInteractedMessage();
}
@override
Widget build(BuildContext context) {
return Text("...");
}
}
포그라운드와 백그라운드 모두 동일하게 onMessageReceived를 통해 개발자가 원하는대로 컨트롤 하려면 Data타입의 푸쉬를 받아야 합니다. 대시보드에 다음과 같이 설정해야 data 타입 푸쉬를 받게 되십니다: