mbnotify-app 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/index.js +19 -6
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.js +18 -5
- package/package.json +1 -1
- package/src/index.ts +19 -8
package/dist/cjs/index.d.ts
CHANGED
package/dist/cjs/index.js
CHANGED
|
@@ -2,15 +2,21 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.requestPermission = requestPermission;
|
|
4
4
|
exports.getToken = getToken;
|
|
5
|
-
const
|
|
5
|
+
const mqtt = require("mqtt/dist/mqtt");
|
|
6
6
|
const Notifications = require("expo-notifications");
|
|
7
7
|
const async_storage_1 = require("@react-native-async-storage/async-storage");
|
|
8
8
|
const DEFAULT_BROKER = "wss://broker.hivemq.com:8884/mqtt";
|
|
9
9
|
let client = null;
|
|
10
|
+
/**
|
|
11
|
+
* Request permission
|
|
12
|
+
*/
|
|
10
13
|
async function requestPermission() {
|
|
11
14
|
const { status } = await Notifications.requestPermissionsAsync();
|
|
12
15
|
return status === "granted";
|
|
13
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Generate token
|
|
19
|
+
*/
|
|
14
20
|
async function generateToken() {
|
|
15
21
|
let token = await async_storage_1.default.getItem("mbnotify_token");
|
|
16
22
|
if (!token) {
|
|
@@ -22,23 +28,27 @@ async function generateToken() {
|
|
|
22
28
|
}
|
|
23
29
|
return token;
|
|
24
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Connect MQTT
|
|
33
|
+
*/
|
|
25
34
|
function connectMQTT(brokerUrl) {
|
|
26
35
|
if (client)
|
|
27
36
|
return client;
|
|
28
|
-
client =
|
|
37
|
+
client = mqtt.connect(brokerUrl, {
|
|
29
38
|
reconnectPeriod: 5000,
|
|
30
39
|
connectTimeout: 10000
|
|
31
40
|
});
|
|
32
41
|
client.on("connect", () => {
|
|
33
|
-
|
|
42
|
+
console.log("✅ MQTT Connected");
|
|
34
43
|
});
|
|
35
44
|
client.on("error", (err) => {
|
|
36
|
-
console.error("MQTT
|
|
45
|
+
console.error("❌ MQTT Error:", err);
|
|
37
46
|
});
|
|
38
|
-
// ✅ SINGLE
|
|
47
|
+
// ✅ SINGLE LISTENER
|
|
39
48
|
client.on("message", async (_, message) => {
|
|
40
49
|
try {
|
|
41
50
|
const payload = JSON.parse(message.toString());
|
|
51
|
+
console.log("📩 Received:", payload);
|
|
42
52
|
await Notifications.scheduleNotificationAsync({
|
|
43
53
|
content: {
|
|
44
54
|
title: payload.title || "",
|
|
@@ -54,6 +64,9 @@ function connectMQTT(brokerUrl) {
|
|
|
54
64
|
});
|
|
55
65
|
return client;
|
|
56
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Get token + subscribe
|
|
69
|
+
*/
|
|
57
70
|
async function getToken(appName, brokerUrl = DEFAULT_BROKER) {
|
|
58
71
|
if (!appName)
|
|
59
72
|
throw new Error("appName required");
|
|
@@ -61,6 +74,6 @@ async function getToken(appName, brokerUrl = DEFAULT_BROKER) {
|
|
|
61
74
|
const topic = `/${appName}/${token}/notification`;
|
|
62
75
|
const mqttClient = connectMQTT(brokerUrl);
|
|
63
76
|
mqttClient.subscribe(topic);
|
|
64
|
-
|
|
77
|
+
console.log("📡 Subscribed:", topic);
|
|
65
78
|
return token;
|
|
66
79
|
}
|
package/dist/esm/index.d.ts
CHANGED
package/dist/esm/index.js
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
import mqtt from "mqtt/dist/mqtt";
|
|
1
|
+
import * as mqtt from "mqtt/dist/mqtt";
|
|
2
2
|
import * as Notifications from "expo-notifications";
|
|
3
3
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
4
4
|
const DEFAULT_BROKER = "wss://broker.hivemq.com:8884/mqtt";
|
|
5
5
|
let client = null;
|
|
6
|
+
/**
|
|
7
|
+
* Request permission
|
|
8
|
+
*/
|
|
6
9
|
export async function requestPermission() {
|
|
7
10
|
const { status } = await Notifications.requestPermissionsAsync();
|
|
8
11
|
return status === "granted";
|
|
9
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Generate token
|
|
15
|
+
*/
|
|
10
16
|
async function generateToken() {
|
|
11
17
|
let token = await AsyncStorage.getItem("mbnotify_token");
|
|
12
18
|
if (!token) {
|
|
@@ -18,6 +24,9 @@ async function generateToken() {
|
|
|
18
24
|
}
|
|
19
25
|
return token;
|
|
20
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Connect MQTT
|
|
29
|
+
*/
|
|
21
30
|
function connectMQTT(brokerUrl) {
|
|
22
31
|
if (client)
|
|
23
32
|
return client;
|
|
@@ -26,15 +35,16 @@ function connectMQTT(brokerUrl) {
|
|
|
26
35
|
connectTimeout: 10000
|
|
27
36
|
});
|
|
28
37
|
client.on("connect", () => {
|
|
29
|
-
|
|
38
|
+
console.log("✅ MQTT Connected");
|
|
30
39
|
});
|
|
31
40
|
client.on("error", (err) => {
|
|
32
|
-
console.error("MQTT
|
|
41
|
+
console.error("❌ MQTT Error:", err);
|
|
33
42
|
});
|
|
34
|
-
// ✅ SINGLE
|
|
43
|
+
// ✅ SINGLE LISTENER
|
|
35
44
|
client.on("message", async (_, message) => {
|
|
36
45
|
try {
|
|
37
46
|
const payload = JSON.parse(message.toString());
|
|
47
|
+
console.log("📩 Received:", payload);
|
|
38
48
|
await Notifications.scheduleNotificationAsync({
|
|
39
49
|
content: {
|
|
40
50
|
title: payload.title || "",
|
|
@@ -50,6 +60,9 @@ function connectMQTT(brokerUrl) {
|
|
|
50
60
|
});
|
|
51
61
|
return client;
|
|
52
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Get token + subscribe
|
|
65
|
+
*/
|
|
53
66
|
export async function getToken(appName, brokerUrl = DEFAULT_BROKER) {
|
|
54
67
|
if (!appName)
|
|
55
68
|
throw new Error("appName required");
|
|
@@ -57,6 +70,6 @@ export async function getToken(appName, brokerUrl = DEFAULT_BROKER) {
|
|
|
57
70
|
const topic = `/${appName}/${token}/notification`;
|
|
58
71
|
const mqttClient = connectMQTT(brokerUrl);
|
|
59
72
|
mqttClient.subscribe(topic);
|
|
60
|
-
|
|
73
|
+
console.log("📡 Subscribed:", topic);
|
|
61
74
|
return token;
|
|
62
75
|
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import mqtt from "mqtt/dist/mqtt";
|
|
1
|
+
import * as mqtt from "mqtt/dist/mqtt";
|
|
2
2
|
import * as Notifications from "expo-notifications";
|
|
3
3
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
4
4
|
|
|
@@ -6,19 +6,22 @@ const DEFAULT_BROKER = "wss://broker.hivemq.com:8884/mqtt";
|
|
|
6
6
|
|
|
7
7
|
let client: any = null;
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Request permission
|
|
11
|
+
*/
|
|
9
12
|
export async function requestPermission(): Promise<boolean> {
|
|
10
|
-
|
|
11
13
|
const { status } = await Notifications.requestPermissionsAsync();
|
|
12
|
-
|
|
13
14
|
return status === "granted";
|
|
14
15
|
}
|
|
15
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Generate token
|
|
19
|
+
*/
|
|
16
20
|
async function generateToken(): Promise<string> {
|
|
17
21
|
|
|
18
22
|
let token = await AsyncStorage.getItem("mbnotify_token");
|
|
19
23
|
|
|
20
24
|
if (!token) {
|
|
21
|
-
|
|
22
25
|
token =
|
|
23
26
|
"dev_" +
|
|
24
27
|
Math.random().toString(36).substring(2) +
|
|
@@ -30,6 +33,9 @@ async function generateToken(): Promise<string> {
|
|
|
30
33
|
return token;
|
|
31
34
|
}
|
|
32
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Connect MQTT
|
|
38
|
+
*/
|
|
33
39
|
function connectMQTT(brokerUrl: string) {
|
|
34
40
|
|
|
35
41
|
if (client) return client;
|
|
@@ -40,20 +46,22 @@ function connectMQTT(brokerUrl: string) {
|
|
|
40
46
|
});
|
|
41
47
|
|
|
42
48
|
client.on("connect", () => {
|
|
43
|
-
|
|
49
|
+
console.log("✅ MQTT Connected");
|
|
44
50
|
});
|
|
45
51
|
|
|
46
52
|
client.on("error", (err: any) => {
|
|
47
|
-
console.error("MQTT
|
|
53
|
+
console.error("❌ MQTT Error:", err);
|
|
48
54
|
});
|
|
49
55
|
|
|
50
|
-
// ✅ SINGLE
|
|
56
|
+
// ✅ SINGLE LISTENER
|
|
51
57
|
client.on("message", async (_: any, message: any) => {
|
|
52
58
|
|
|
53
59
|
try {
|
|
54
60
|
|
|
55
61
|
const payload = JSON.parse(message.toString());
|
|
56
62
|
|
|
63
|
+
console.log("📩 Received:", payload);
|
|
64
|
+
|
|
57
65
|
await Notifications.scheduleNotificationAsync({
|
|
58
66
|
content: {
|
|
59
67
|
title: payload.title || "",
|
|
@@ -72,6 +80,9 @@ function connectMQTT(brokerUrl: string) {
|
|
|
72
80
|
return client;
|
|
73
81
|
}
|
|
74
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Get token + subscribe
|
|
85
|
+
*/
|
|
75
86
|
export async function getToken(
|
|
76
87
|
appName: string,
|
|
77
88
|
brokerUrl: string = DEFAULT_BROKER
|
|
@@ -87,7 +98,7 @@ export async function getToken(
|
|
|
87
98
|
|
|
88
99
|
mqttClient.subscribe(topic);
|
|
89
100
|
|
|
90
|
-
|
|
101
|
+
console.log("📡 Subscribed:", topic);
|
|
91
102
|
|
|
92
103
|
return token;
|
|
93
104
|
}
|