Yes it's possible for Andorid and iOS, but React Native is not possible. Here example code for Android and iOS:
Android
You can follow this steps to able to mute notification:
- Create singleton class ChatPreferencesHelper for handle Mute preferences
public class ChatPreferencesHelper {
private static ChatPreferencesHelper INSTANCE;
private SharedPreferences sharedPreferences;
private ChatPreferencesHelper() {
sharedPreferences = App.getInstance()
.getSharedPreferences("chat.qisme.sp", Context.MODE_PRIVATE);
}
public static ChatPreferencesHelper getInstance() {
if (INSTANCE == null) {
INSTANCE = new ChatPreferencesHelper();
}
return INSTANCE;
}
public void setEnableNotification(boolean enable) {
sharedPreferences.edit().putBoolean("enable_pn_default", enable).apply();
}
public boolean isEnableNotification() {
return sharedPreferences.getBoolean("enable_pn_default", true);
}
public void setEnableNotification(int qiscusRoomId, boolean enable) {
sharedPreferences.edit().putBoolean("enable_pn_" + qiscusRoomId, enable).apply();
}
public boolean isEnableNotification(int qiscusRoomId) {
return sharedPreferences.getBoolean("enable_pn_" + qiscusRoomId, isEnableNotification());
}
}
2. Intercept Notification using Qiscus.getChatConfig()
Qiscus.getChatConfig()
.setNotificationBuilderInterceptor((builder, qiscusComment) -> {
/**
* check mute for all room
* ChatPreferencesHelper.getInstance().isEnableNotification()
*
* check mute per room
* !ChatPreferencesHelper.getInstance().isEnableNotification(roomId)
*/
if (ChatPreferencesHelper.getInstance().isEnableNotification()
|| !ChatPreferencesHelper.getInstance()
.isEnableNotification(qiscusComment.getRoomId())) {
//return false for mute
return false;
}
/**
* TODO other implementation
*/
return true;
});
3.Implementation, call this method for mute room:
/**
* call this method for mute all room
*/
ChatPreferencesHelper.getInstance().setEnableNotification(true);
/**
* call this method for mute per room
*/
ChatPreferencesHelper.getInstance().setEnableNotification(qiscusRoomId, true);
IOS
Implement Silent Notification iOS using Pushkit only, You can follow this steps to able to mute notification:
- Create class to handle UserDefault
class NotifPreference {
private let pref = UserDefaults.standard
static let shared = NotifPreference()
private init() {
}
/// Add muted room id
///
/// - Parameter roomId: qiscus room id
func addMutedRoom(roomId: String) {
var mutedRooms = getMutedRoom()
mutedRooms.append(roomId)
pref.set(mutedRooms, forKey: "muted_room")
}
/// Check is room with roomId has been muted
///
/// - Parameter roomId: qiscus room id
/// - Returns: true if room with current id is muted
func isRoomMuted(roomId: String) -> Bool {
let mutedRooms = getMutedRoom()
return mutedRooms.contains(roomId)
}
/// Remove room id from muted room id list
///
/// - Parameter roomId: qiscus room id
func removeMutedRoom(roomId: String) {
var mutedRooms = getMutedRoom()
if mutedRooms.count != 0 {
var i = 0
for mutedRoomid in mutedRooms {
if mutedRoomid == roomId {
break
}
i += 1
}
mutedRooms.remove(at: i)
self.pref.set(mutedRooms, forKey: "muted_room")
}
}
/// Get list of muted room id
///
/// - Returns: array of muted room id
func getMutedRoom() -> [String] {
if let array = pref.array(forKey: "muted_room") as? [String] {
return array
} else {
return []
}
}
}
2. Get room Id from your Chat Room list, and add to “muted_room” userDefault
let roomId = rooms[selectedIndex].id // get room id
// mute room by room id
NotifPreference.shared.addMutedRoom(roomId: roomId)
// unmuted room by room id
NotifPreference.shared.removeMutedRoom(roomId: roomId)
3. Handle incoming push notification
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) {
// Get room id from notification userInfo
if let payload = payload.dictionaryPayload["payload"] {
let payloadData = payload as! [String : Any]
let roomId = payloadData["room_id"]
if !NotifPreference.shared.isRoomMuted(roomId: roomId!) {
// TODO: CREATE LOCAL NOTIFICATION HERE
}
}
}
Comments
0 comments
Please sign in to leave a comment.