Set custom push rules
If a user is logged in on multiple platforms, the user can configure custom push rules to specify the platforms that can or cannot receive offline push notifications.
Prerequisites
Before you implement the custom push rule feature, make sure that the following conditions are met:
- Multi-device login is implemented. For more information, see Multi-device login.
Implementation steps
Notification for offline push rule update
To receive notifications when users customize rules, please listen for the onUserRuleUpdated callback.
// Listen for user-defined rules
@Override
public void onUserRuleUpdated(ZIM zim, ZIMUserRule rule) {
// Offline push rules
userRule.offlinePushRule;
}Set offline push rules
To support custom push rules, developers need to construct ZIMUserOfflinePushRule to specify the platforms that do not receive offline push notifications (notToReceiveOfflinePushPlatforms) when the user is online on specified platforms (onlinePlatforms).
To set the above rules, call the updateUserOfflinePushRule interface.
// Construct ZIMUserOfflinePushRule
ArrayList<Integer> onlinePlatforms = new ArrayList<>();
// When the user logs in on Windows, macOS, and Linux platforms.
ArrayList<Integer> onlinePlatforms = new ArrayList<>();
onlinePlatforms.add(ZIMPlatformType.WIN.value());
onlinePlatforms.add(ZIMPlatformType.MAC_OS.value());
onlinePlatforms.add(ZIMPlatformType.LINUX.value());
offlineRule.setOnlinePlatforms(onlinePlatforms);
// The user does not receive offline push on the iPhoneOS, iPadOS, and Android platforms.
ArrayList<Integer> notToReceiveOfflinePushPlatforms = new ArrayList<>();
notToReceiveOfflinePushPlatforms.add(ZIMPlatformType.IPHONE_OS.value());
notToReceiveOfflinePushPlatforms.add(ZIMPlatformType.IPAD_OS.value());
notToReceiveOfflinePushPlatforms.add(ZIMPlatformType.ANDROID.value());
offlineRule.setNotToReceiveOfflinePushPlatforms(notToReceiveOfflinePushPlatforms);
// Call the updateUserOfflinePushRule method
ZIM.getInstance().updateUserOfflinePushRule(offlineRule, new ZIMUserOfflinePushRuleUpdatedCallback() {
@Override
public void onUserOfflinePushRuleUpdated(ZIMUserOfflinePushRule updatedOfflinePushRule, ZIMError errorInfo) {
}
});Query user-defined push rules
By calling querySelfUserInfo, you can get the current user-defined offline push rules in the selfUserInfo.userRule.offlinePushRule field in the callback.
ZIM.getInstance().querySelfUserInfo(new ZIMSelfUserInfoQueriedCallback() {
@Override
public void onSelfUserInfoQueried(ZIMSelfUserInfo selfUserInfo, ZIMError errorInfo) {
selfUserInfo.userRule.offlinePushRule; // Current user-defined offline push rules
}
});