Implementing JPush Push Notifications for Google Play Version in Flutter
Basic JPush Integration
- Add the depandency in
pubspec.yaml:
dependencies:
jpush_flutter: ^2.4.2
- Create a JPush utility class:
class PushNotificationManager {
static initialize() {
JPush().addEventHandler(
onReceiveNotification: (Map message) async {
print('Notification received: $message');
},
onOpenNotification: (Map message) async {
print('Notification opened: $message');
resetBadge();
},
onNotifyMessageUnShow: (Map message) async {
resetBadge();
},
);
JPush().applyPushAuthority(
NotificationSettingsIOS(sound: true, alert: true, badge: true)
);
JPush().setup(
appKey: 'your_app_key',
channel: 'developer-default',
production: false,
debug: true
);
}
static updateTags(List<String> tags) {
JPush().setTags(tags);
}
static setUserAlias(String alias) {
JPush().setAlias(alias);
}
static removeAlias() {
JPush().deleteAlias();
}
static clearTags() {
JPush().cleanTags();
}
static resetBadge([int count = 0]) {
JPush().setBadge(count);
}
}
- Configure
build.gradle:
android {
defaultConfig {
manifestPlaceholders = [
JPUSH_PKGNAME: "your.package.name",
JPUSH_APPKEY: "your_app_key",
JPUSH_CHANNEL: "developer-default",
// Manufacturer-specific keys
XIAOMI_APPID: "",
XIAOMI_APPKEY: "",
OPPO_APPID: "",
OPPO_APPKEY: "",
OPPO_APPSECRET: "",
VIVO_APPID: "",
VIVO_APPKEY: ""
]
}
}
Google Play Version Modifications
- Dwonload the Google Play version of JPush SDK matching your plugin version
- Replace the SDK files in
jpush_flutter/android/libs/ - Modify the plugin's
build.gradle:
dependencies {
implementation 'cn.jiguang.sdk:jpush:4.9.0'
compile files('libs/jcore-android-4.1.0-google_play.jar')
}
For JPush 5.0+ versions, use:
implementation('cn.jiguang.sdk:jpush:5.4.0') {
exclude module: 'jcore'
}
compile files('libs/jcore-android-4.7.0-google_play.jar')
This configurasion resolves duplicate class conflicts while maintaining push notification functionality for Google Play distribution.