- 极光推送注意事项
- 个人开发者每天免费 10 条广播推送,单点设备推送不限
flutter 端配置
配置 project\android\app\build.gradle
defaultConfig{
...
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "Appkey", // NOTE: JPush 上注册的包名对应的 Appkey.
JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
]
}
配置 project\android\app\src\main\AndroidManifest.xml
权限配置
<uses-permission android:name="android.permission.INTERNET"/>
安装依赖及案例
jpush_flutter: 3.0.0
案例
import 'package:flutter/material.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
void main(){
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: PageApp(),
);
}
}
class PageApp extends StatefulWidget {
const PageApp({super.key});
@override
State<PageApp> createState() => _PageAppState();
}
class _PageAppState extends State<PageApp> {
@override
void initState(){
super.initState();
initJpush();
}
//监听极光推送 (自定义的方法)
//https://github.com/jpush/jpush-flutter-plugin/blob/master/documents/APIs.md
initJpush() async {
JPush jpush = new JPush();
//获取注册的id
jpush.getRegistrationID().then((rid) {
print("获取注册的id:$rid");
});
//初始化
jpush.setup(
appKey: "appkey",
channel: "theChannel",
production: false,
debug: true, // 设置是否打印 debug 日志
);
//设置别名 实现指定用户推送
jpush.setAlias("jg123").then((map) {
print("设置别名成功");
});
try {
//监听消息通知
jpush.addEventHandler(
// 接收通知回调方法。
onReceiveNotification: (Map<String, dynamic> message) async {
print("flutter onReceiveNotification: $message");
},
// 点击通知回调方法。
onOpenNotification: (Map<String, dynamic> message) async {
print("flutter onOpenNotification: $message");
},
// 接收自定义消息回调方法。
onReceiveMessage: (Map<String, dynamic> message) async {
print("flutter onReceiveMessage: $message");
},
);
} catch (e) {
print('极光sdk配置异常');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title:Text("flutter推送")),
);
}
}
上面代码中可以通过 jpush.setAlias
方式为设备设置别名,用于后期的单点推送
本地端推送
JPush jpush = new JPush();
var fireDate = DateTime.fromMillisecondsSinceEpoch(DateTime.now().millisecondsSinceEpoch + 3000);
var localNotification = LocalNotification(
id: 234,
title: '本地应用推送',
buildId: 1,
content: '定时任务本地消息推送',
fireTime: fireDate,
subtitle: 'notification subtitle', // 该参数只有在 iOS 有效
badge: 5, // 该参数只有在 iOS 有效
);
jpush.sendLocalNotification(localNotification).then((res) {});
Node端调用极光推送
nest端
app.service.ts
import { Injectable } from '@nestjs/common';
import { JPushAsync } from "jpush-async";
@Injectable()
export class AppService {
async getHello() {
console.log(JPushAsync)
var client = JPushAsync.buildClient('appKey', 'masterSecret')
client.push().setPlatform('ios', 'android')
.setAudience(JPushAsync.alias('jg123'))
.setNotification(JPushAsync.android('龚箭向你发送了一条信息', '定点消息推送测试', 1, {'key':'value'}))
.send()
.then(function(result) {
console.log(result)
}).catch(function(err) {
console.log(err)
})
return {
code:101,
message:'hello world',
data:null
};
}
}
上面代码中 setAudience(JPushAsync.alias('jg123'))
中 JPushAsync.alias('jg123')
为设备的别名,同时支持多设备推送示例JPushAsync.alias('xxx1','xxx2',...)
广播推送.setAudience(JPushAsync.ALL)
app.controller.ts
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello() {
return this.appService.getHello();
}
}