返回

一文看懂 Uniapp 极光推送集成指南 | 超简单 2 行代码搞定

Android

Uniapp 极光推送:轻松集成,畅享移动推送

对于移动应用程序开发者来说,推送通知是与用户保持联系、传递重要消息和促进应用参与度的有效方式。极光推送作为一款强大的移动推送服务,以其可靠性、高效性和丰富的功能备受青睐。Uniapp 框架的普及让更多开发者选择使用它进行跨平台开发。为了满足 Uniapp 开发者的需求,我们开发了一款 Android + iOS 统一的极光推送插件,旨在简化极光推送的集成,统一数据处理,让开发者轻松享受极光推送带来的便捷。

集成步骤

1. 安装插件

  • Android:
npm install @uni-extend/jpush --save
  • iOS:
pod 'JPush', '~> 4.0.5'

2. 配置极光推送 AppKey 和 AppSecret

  • Android:
<meta-data
    android:name="JPUSH_APPKEY"
    android:value="你的 AppKey" />
<meta-data
    android:name="JPUSH_CHANNEL"
    android:value="你的 Channel" />
  • iOS:
<key>JPUSH_APPKEY</key>
<string>你的 AppKey</string>
<key>JPUSH_CHANNEL</key>
<string>你的 Channel</string>

3. 初始化插件

  • Android:
import cn.jiguang.jpush.PushManager;

...

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PushManager.getInstance().init(this);
    }
}
  • iOS:
#import "AppDelegate.h"
#import <JPush/JPUSHService.h>

...

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [JPUSHService setupWithOption:launchOptions appKey:@"你的 AppKey" channel:@"你的 Channel" apsForProduction:NO];
    return YES;
}

4. 注册监听器

  • Android:
import cn.jiguang.jpush.api.JPushInterface;

...

JPushInterface.setPushNotificationActionListener(new JPushInterface.PushNotificationActionListener() {
    @Override
    public void onNotificationOpened(Context context, String message, String extras) {
        // TODO 处理点击通知后的操作
    }

    @Override
    public void onNotificationMessageArrived(Context context, JPushInterface.RichPushMessage message) {
        // TODO 处理收到通知消息后的操作
    }

    @Override
    public void onNotificationSettingsChanged(Context context, JPushInterface.PushNotificationSettings settings) {
        // TODO 处理通知设置发生变化后的操作
    }
});
  • iOS:
#import <JPush/JPUSHService.h>

...

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    NSDictionary *userInfo = notification.request.content.userInfo;
    // TODO 处理通知弹出的操作
    completionHandler(UNNotificationPresentationOptionAlert);
}

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    // TODO 处理点击通知后的操作
    completionHandler();
}

总结

通过上述步骤,你就可以轻松集成 Uniapp 极光推送自定义消息和通知,只需 2 行代码即可搞定 Android 和 iOS 极光 SDK 的集成,让你的 App 轻松接收推送。希望本文对你有所帮助,欢迎留言交流。

常见问题解答

1. 如何处理通知点击事件?

  • Android: 覆写 onNotificationOpened 方法即可处理通知点击事件。
  • iOS: 覆写 didReceiveNotificationResponse 方法即可处理通知点击事件。

2. 如何处理通知消息?

  • Android: 覆写 onNotificationMessageArrived 方法即可处理通知消息。
  • iOS: jpushNotificationCenter 回调方法中有收到通知消息后的处理逻辑。

3. 如何设置通知样式?

  • Android: 使用 JPushInterfacesetNotificationBuilder 方法即可设置通知样式。
  • iOS: 使用 JPUSHServiceregisterForRemoteNotificationTypes 方法即可设置通知样式。

4. 如何统计推送效果?

  • 极光推送提供了一系列统计报表,登录极光推送管理后台即可查看详细数据。

5. 是否支持离线推送?

  • 是的,极光推送支持离线推送,当用户设备不在线时,会在用户上线后自动推送。