I have implemented Cometchat SDK into my app, and i also followed all the requirements going through firebase to add the server legacy key into Cometchat console and subscribe to channel using "push_channel" and use the below command:
Messaging.messaging().subscribe(toTopic: push_channel)
Lets say that iOS device send a message to another iOS device or even tried to call, and the app was turned off, no notification happen to notify the other device that a call or message is being received, until the user opens the app and then open CometChat to be able to chat or see messages, am i missing something ? is Cometchat is a VOIP that should awake the app on call ? or not ?
Update:
We tried on sending notification to targeted device fcm token and it’s working fine and being received successfully.( through firebase console).
We also tried on sending notification to topic which is the push_channel
through fire base and notifications is not being received to any device. ( through firebase console).
We are sure that we implemented all the required information in firebase and cometchat console.
Concern: I have a concern but should not be the problem but I will say that we are using push notification key instead of the p12 in firebase cloud messages certificates but that should not be the problem though ?
Code and Update
We added the server legacy key from firebase into Cometchat > Settings > Mobile, since long time and its still not working.
The below code is the class that I'm using to communicate or instantiate Cometchat and register the token, tell me what I'm doing wrong though, even you don't have a full covering sample in your documentation so here you go:
This is CometChatHandler class :
fileprivate let cometChat: CometChat = CometChat()
fileprivate let readyUI: readyUIFIle = readyUIFIle()
fileprivate let isCometOnDemand: Bool = true // For CometChat Cloud Users, Please set this to true
init(apiKey: String, licenseKey: String) {
cometChat.initializeCometChat("", licenseKey: licenseKey, apikey: apiKey, isCometOnDemand: isCometOnDemand, success: {(response) in
}, failure: { (error) in
})
}
func login(userUuid: String, userName: String) {
if self.cometChat.isUserLoggedIn() {
print("user already login")
} else {
let UID = "User_\(userUuid)"
cometChat.login(withUID: UID, success: { (dictionary: [AnyHashable: Any]!) -> () in
}, failure: { (error: Error!) -> () in
self.createUser(userUuid: userUuid, userName: userName)
})
}
}
func logout() {
cometChat.logout({ dictionary in
}, failure: { error in
})
}
func startChat(userUuid: String, delegate: UIViewController) {
if self.cometChat.isUserLoggedIn() {
self.cometChat.subscribeCallbacks(true, onMyInfoReceived: { (response) in
print("onMyInfoReceived \(String(describing: response))")
if let res = response as? Dictionary<String,Any>{
let push_channel = res["push_channel"] as? String ?? ""
print(push_channel)
DispatchQueue.main.async {
Messaging.messaging().subscribe(toTopic: push_channel)
}
}
}, onUserListReceived: { (response) in
print("onUserListReceived \(String(describing: response))")
}, onMessageReceived: { (response) in
print("onMessageReceived \(String(describing: response))")
}, onAVChatMessageReceived: { (response) in
print("onAVChatMessageReceived \(String(describing: response))")
}, onActionMessageReceived: { (response) in
print("onActionMessageReceived \(String(describing: response))")
}, onGroupListReceived: { (response) in
print("onGroupListReceived \(String(describing: response))")
}, onGroupMessageReceived: { (response) in
print("onGroupMessageReceived \(String(describing: response))")
}, onGroupAVChatMessageReceived: { (response) in
print("onGroupAVChatMessageReceived \(String(describing: response))")
}, onGroupActionMessageReceived: { (response) in
print("onGroupActionMessageReceived \(String(describing: response))")
}, onRecentChatListReceived: { (response) in
print("onRecentChatListReceived \(String(describing: response))")
}, onAnnouncementReceived: { (response) in
print("onAnnouncementReceived \(String(describing: response))")
}) { (error) in
}
openChatUI(userUuid: userUuid, delegate: delegate)
} else {
print("User not login")
}
}
#warning("TODO for left to right")
private func openChatUI(userUuid: String, delegate: UIViewController, isGroup: Bool = false, isFullScreen: Bool = true) {
self.readyUI.launchCometChat(userUuid, isGroup: isGroup,
isFullScreen: isFullScreen,
observer: delegate,
setBackButton: true, userInfo: { (response) in
print("Success Login with these data \(String(describing: response))")
}, groupInfo: { (response) in
print("Success groupInfo with these data \(String(describing: response))")
}, onMessageReceive: { (response) in
print("Success onMessageReceive with these data \(String(describing: response))")
}, success: { (response) in
print("Success success with these data \(String(describing: response))")
}, failure: { (error) in
}, onLogout: { (response) in
print("Success onLogout with these data \(String(describing: response))")
})
}
private func createUser(userUuid: String, userName: String) {
let UID = "User_\(userUuid)"
cometChat.createUser(UID,
userName: userName,
avatarUrl: "",
profileUrl: "",
role: "",
success: { (_) in
self.login(userUuid: userUuid, userName: userName)
}) { (error) in
print(error?.localizedDescription ?? "")
}
}
To login a user when start using my app, i login the user with Cometchat:
cometChatHolder.login(userUuid: "\(id)", userName: username)
Then later on anytime to start a chat and launch the chat i call:
cometChatHolder.startChat(userUuid: "the id of the user to start the chat with", delegate: self)
I'm really in a dead-end and i hope i can get some help to fix this out, because i'm so out of options.
Update (Steps to fix): This is how i fixed the issue, don't rely on the SDK telling you that user is logged in, so every time you should initiate the cometChat and then login the user and then open the chat, what i was doing is checking if the user logged in through the SDK and then open the SDK, thats the tip in fixing the issue.
It seems that you have not added the Firebase Legacy key in CometChat Admin Panel. Please follow the below steps:
Could you also please check if the following points have been implemented properly? These are the ones responsible for receiving push notifications from Firebase -
Subscribing to a channel from where you will get the push notification. You will get this channel from the response received in onMyInfoReceived() callback when you call the subscribe method using the SDK.The response of callbacks contains a key named as “push_channel”. This contains the push notification channel. Once you subscribe to this channel, you will start receiving push notifications for one-on-one chat. You can subscribe to the channel received using the following method:
[[FIRMessaging messaging] subscribeToTopic:[NSString stringWithFormat:@"%@",push_channel]];
Please refer to the attached image for a better understanding.
For further resources, you can refer to the documentation for push notifications in iOS: https://developer.cometchat.com/docs/ios-push-notifications
UPDATE ON DECEMBER, 4:
We've created a Sample App and have added Firebase Push Notifications to it. You can download the project from the below URL:
https://temp.cometchat.com/ios-swift-chat-sdk-demo-master.zip
Please refer to the code present in ViewController.swift and AppDelegate.swift files.
Hope this resolves the issue. Let us know if the problem still persists.
Disclosure - This account is owned by the CometChat team.