mbnotify-app 1.0.0 → 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 +2 -10
- package/dist/cjs/index.js +28 -33
- package/dist/esm/index.d.ts +2 -10
- package/dist/esm/index.js +27 -32
- package/package.json +6 -4
- package/src/index.ts +34 -57
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,16 +1,8 @@
|
|
|
1
|
-
export interface NotificationPayload {
|
|
2
|
-
title?: string;
|
|
3
|
-
body?: string;
|
|
4
|
-
icon?: string;
|
|
5
|
-
image?: string;
|
|
6
|
-
url?: string;
|
|
7
|
-
data?: Record<string, any>;
|
|
8
|
-
}
|
|
9
1
|
/**
|
|
10
|
-
* Request
|
|
2
|
+
* Request permission
|
|
11
3
|
*/
|
|
12
4
|
export declare function requestPermission(): Promise<boolean>;
|
|
13
5
|
/**
|
|
14
|
-
*
|
|
6
|
+
* Get token + subscribe
|
|
15
7
|
*/
|
|
16
8
|
export declare function getToken(appName: string, brokerUrl?: string): Promise<string>;
|
package/dist/cjs/index.js
CHANGED
|
@@ -2,20 +2,20 @@
|
|
|
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
10
|
/**
|
|
11
|
-
* Request
|
|
11
|
+
* Request permission
|
|
12
12
|
*/
|
|
13
13
|
async function requestPermission() {
|
|
14
14
|
const { status } = await Notifications.requestPermissionsAsync();
|
|
15
15
|
return status === "granted";
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
|
-
* Generate
|
|
18
|
+
* Generate token
|
|
19
19
|
*/
|
|
20
20
|
async function generateToken() {
|
|
21
21
|
let token = await async_storage_1.default.getItem("mbnotify_token");
|
|
@@ -29,56 +29,51 @@ async function generateToken() {
|
|
|
29
29
|
return token;
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
32
|
-
* Connect MQTT
|
|
32
|
+
* Connect MQTT
|
|
33
33
|
*/
|
|
34
34
|
function connectMQTT(brokerUrl) {
|
|
35
35
|
if (client)
|
|
36
36
|
return client;
|
|
37
|
-
client =
|
|
37
|
+
client = mqtt.connect(brokerUrl, {
|
|
38
38
|
reconnectPeriod: 5000,
|
|
39
39
|
connectTimeout: 10000
|
|
40
40
|
});
|
|
41
41
|
client.on("connect", () => {
|
|
42
|
-
console.log("
|
|
42
|
+
console.log("✅ MQTT Connected");
|
|
43
43
|
});
|
|
44
44
|
client.on("error", (err) => {
|
|
45
|
-
console.error("MQTT
|
|
45
|
+
console.error("❌ MQTT Error:", err);
|
|
46
46
|
});
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
47
|
+
// ✅ SINGLE LISTENER
|
|
48
|
+
client.on("message", async (_, message) => {
|
|
49
|
+
try {
|
|
50
|
+
const payload = JSON.parse(message.toString());
|
|
51
|
+
console.log("📩 Received:", payload);
|
|
52
|
+
await Notifications.scheduleNotificationAsync({
|
|
53
|
+
content: {
|
|
54
|
+
title: payload.title || "",
|
|
55
|
+
body: payload.body || "",
|
|
56
|
+
data: payload.data || {}
|
|
57
|
+
},
|
|
58
|
+
trigger: null
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
console.error("Invalid payload", err);
|
|
63
|
+
}
|
|
60
64
|
});
|
|
65
|
+
return client;
|
|
61
66
|
}
|
|
62
67
|
/**
|
|
63
|
-
*
|
|
68
|
+
* Get token + subscribe
|
|
64
69
|
*/
|
|
65
70
|
async function getToken(appName, brokerUrl = DEFAULT_BROKER) {
|
|
66
|
-
if (!appName)
|
|
71
|
+
if (!appName)
|
|
67
72
|
throw new Error("appName required");
|
|
68
|
-
}
|
|
69
73
|
const token = await generateToken();
|
|
70
74
|
const topic = `/${appName}/${token}/notification`;
|
|
71
75
|
const mqttClient = connectMQTT(brokerUrl);
|
|
72
76
|
mqttClient.subscribe(topic);
|
|
73
|
-
console.log("Subscribed:", topic);
|
|
74
|
-
mqttClient.on("message", async (_, message) => {
|
|
75
|
-
try {
|
|
76
|
-
const payload = JSON.parse(message.toString());
|
|
77
|
-
await showNotification(payload);
|
|
78
|
-
}
|
|
79
|
-
catch (err) {
|
|
80
|
-
console.error("Invalid notification payload", err);
|
|
81
|
-
}
|
|
82
|
-
});
|
|
77
|
+
console.log("📡 Subscribed:", topic);
|
|
83
78
|
return token;
|
|
84
79
|
}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,16 +1,8 @@
|
|
|
1
|
-
export interface NotificationPayload {
|
|
2
|
-
title?: string;
|
|
3
|
-
body?: string;
|
|
4
|
-
icon?: string;
|
|
5
|
-
image?: string;
|
|
6
|
-
url?: string;
|
|
7
|
-
data?: Record<string, any>;
|
|
8
|
-
}
|
|
9
1
|
/**
|
|
10
|
-
* Request
|
|
2
|
+
* Request permission
|
|
11
3
|
*/
|
|
12
4
|
export declare function requestPermission(): Promise<boolean>;
|
|
13
5
|
/**
|
|
14
|
-
*
|
|
6
|
+
* Get token + subscribe
|
|
15
7
|
*/
|
|
16
8
|
export declare function getToken(appName: string, brokerUrl?: string): Promise<string>;
|
package/dist/esm/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import mqtt from "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
6
|
/**
|
|
7
|
-
* Request
|
|
7
|
+
* Request permission
|
|
8
8
|
*/
|
|
9
9
|
export async function requestPermission() {
|
|
10
10
|
const { status } = await Notifications.requestPermissionsAsync();
|
|
11
11
|
return status === "granted";
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
|
-
* Generate
|
|
14
|
+
* Generate token
|
|
15
15
|
*/
|
|
16
16
|
async function generateToken() {
|
|
17
17
|
let token = await AsyncStorage.getItem("mbnotify_token");
|
|
@@ -25,7 +25,7 @@ async function generateToken() {
|
|
|
25
25
|
return token;
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
-
* Connect MQTT
|
|
28
|
+
* Connect MQTT
|
|
29
29
|
*/
|
|
30
30
|
function connectMQTT(brokerUrl) {
|
|
31
31
|
if (client)
|
|
@@ -35,46 +35,41 @@ function connectMQTT(brokerUrl) {
|
|
|
35
35
|
connectTimeout: 10000
|
|
36
36
|
});
|
|
37
37
|
client.on("connect", () => {
|
|
38
|
-
console.log("
|
|
38
|
+
console.log("✅ MQTT Connected");
|
|
39
39
|
});
|
|
40
40
|
client.on("error", (err) => {
|
|
41
|
-
console.error("MQTT
|
|
41
|
+
console.error("❌ MQTT Error:", err);
|
|
42
42
|
});
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
43
|
+
// ✅ SINGLE LISTENER
|
|
44
|
+
client.on("message", async (_, message) => {
|
|
45
|
+
try {
|
|
46
|
+
const payload = JSON.parse(message.toString());
|
|
47
|
+
console.log("📩 Received:", payload);
|
|
48
|
+
await Notifications.scheduleNotificationAsync({
|
|
49
|
+
content: {
|
|
50
|
+
title: payload.title || "",
|
|
51
|
+
body: payload.body || "",
|
|
52
|
+
data: payload.data || {}
|
|
53
|
+
},
|
|
54
|
+
trigger: null
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
console.error("Invalid payload", err);
|
|
59
|
+
}
|
|
56
60
|
});
|
|
61
|
+
return client;
|
|
57
62
|
}
|
|
58
63
|
/**
|
|
59
|
-
*
|
|
64
|
+
* Get token + subscribe
|
|
60
65
|
*/
|
|
61
66
|
export async function getToken(appName, brokerUrl = DEFAULT_BROKER) {
|
|
62
|
-
if (!appName)
|
|
67
|
+
if (!appName)
|
|
63
68
|
throw new Error("appName required");
|
|
64
|
-
}
|
|
65
69
|
const token = await generateToken();
|
|
66
70
|
const topic = `/${appName}/${token}/notification`;
|
|
67
71
|
const mqttClient = connectMQTT(brokerUrl);
|
|
68
72
|
mqttClient.subscribe(topic);
|
|
69
|
-
console.log("Subscribed:", topic);
|
|
70
|
-
mqttClient.on("message", async (_, message) => {
|
|
71
|
-
try {
|
|
72
|
-
const payload = JSON.parse(message.toString());
|
|
73
|
-
await showNotification(payload);
|
|
74
|
-
}
|
|
75
|
-
catch (err) {
|
|
76
|
-
console.error("Invalid notification payload", err);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
73
|
+
console.log("📡 Subscribed:", topic);
|
|
79
74
|
return token;
|
|
80
75
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mbnotify-app",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "React Native (Expo) client for mbnotify MQTT push notifications",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -27,11 +27,13 @@
|
|
|
27
27
|
"author": "Manoj Gowda",
|
|
28
28
|
"license": "ISC",
|
|
29
29
|
"dependencies": {
|
|
30
|
+
"@react-native-async-storage/async-storage": "2.2.0",
|
|
31
|
+
"buffer": "^6.0.3",
|
|
32
|
+
"expo-notifications": "~55.0.12",
|
|
30
33
|
"mqtt": "^5.15.0",
|
|
31
|
-
"
|
|
32
|
-
"@react-native-async-storage/async-storage": "^1.23.1"
|
|
34
|
+
"process": "^0.11.10"
|
|
33
35
|
},
|
|
34
36
|
"devDependencies": {
|
|
35
37
|
"typescript": "^5.9.3"
|
|
36
38
|
}
|
|
37
|
-
}
|
|
39
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,39 +1,27 @@
|
|
|
1
|
-
import mqtt from "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
|
|
|
5
5
|
const DEFAULT_BROKER = "wss://broker.hivemq.com:8884/mqtt";
|
|
6
6
|
|
|
7
|
-
let client:
|
|
8
|
-
|
|
9
|
-
export interface NotificationPayload {
|
|
10
|
-
title?: string
|
|
11
|
-
body?: string
|
|
12
|
-
icon?: string
|
|
13
|
-
image?: string
|
|
14
|
-
url?: string
|
|
15
|
-
data?: Record<string, any>
|
|
16
|
-
}
|
|
7
|
+
let client: any = null;
|
|
17
8
|
|
|
18
9
|
/**
|
|
19
|
-
* Request
|
|
10
|
+
* Request permission
|
|
20
11
|
*/
|
|
21
12
|
export async function requestPermission(): Promise<boolean> {
|
|
22
|
-
|
|
23
13
|
const { status } = await Notifications.requestPermissionsAsync();
|
|
24
|
-
|
|
25
14
|
return status === "granted";
|
|
26
15
|
}
|
|
27
16
|
|
|
28
17
|
/**
|
|
29
|
-
* Generate
|
|
18
|
+
* Generate token
|
|
30
19
|
*/
|
|
31
20
|
async function generateToken(): Promise<string> {
|
|
32
21
|
|
|
33
22
|
let token = await AsyncStorage.getItem("mbnotify_token");
|
|
34
23
|
|
|
35
24
|
if (!token) {
|
|
36
|
-
|
|
37
25
|
token =
|
|
38
26
|
"dev_" +
|
|
39
27
|
Math.random().toString(36).substring(2) +
|
|
@@ -46,9 +34,9 @@ async function generateToken(): Promise<string> {
|
|
|
46
34
|
}
|
|
47
35
|
|
|
48
36
|
/**
|
|
49
|
-
* Connect MQTT
|
|
37
|
+
* Connect MQTT
|
|
50
38
|
*/
|
|
51
|
-
function connectMQTT(brokerUrl: string)
|
|
39
|
+
function connectMQTT(brokerUrl: string) {
|
|
52
40
|
|
|
53
41
|
if (client) return client;
|
|
54
42
|
|
|
@@ -58,43 +46,49 @@ function connectMQTT(brokerUrl: string): mqtt.MqttClient {
|
|
|
58
46
|
});
|
|
59
47
|
|
|
60
48
|
client.on("connect", () => {
|
|
61
|
-
console.log("
|
|
49
|
+
console.log("✅ MQTT Connected");
|
|
62
50
|
});
|
|
63
51
|
|
|
64
|
-
client.on("error", (err) => {
|
|
65
|
-
console.error("MQTT
|
|
52
|
+
client.on("error", (err: any) => {
|
|
53
|
+
console.error("❌ MQTT Error:", err);
|
|
66
54
|
});
|
|
67
55
|
|
|
68
|
-
|
|
69
|
-
|
|
56
|
+
// ✅ SINGLE LISTENER
|
|
57
|
+
client.on("message", async (_: any, message: any) => {
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
|
|
61
|
+
const payload = JSON.parse(message.toString());
|
|
62
|
+
|
|
63
|
+
console.log("📩 Received:", payload);
|
|
64
|
+
|
|
65
|
+
await Notifications.scheduleNotificationAsync({
|
|
66
|
+
content: {
|
|
67
|
+
title: payload.title || "",
|
|
68
|
+
body: payload.body || "",
|
|
69
|
+
data: payload.data || {}
|
|
70
|
+
},
|
|
71
|
+
trigger: null
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
} catch (err) {
|
|
75
|
+
console.error("Invalid payload", err);
|
|
76
|
+
}
|
|
70
77
|
|
|
71
|
-
/**
|
|
72
|
-
* Show notification using Expo
|
|
73
|
-
*/
|
|
74
|
-
async function showNotification(payload: NotificationPayload) {
|
|
75
|
-
|
|
76
|
-
await Notifications.scheduleNotificationAsync({
|
|
77
|
-
content: {
|
|
78
|
-
title: payload.title || "",
|
|
79
|
-
body: payload.body || "",
|
|
80
|
-
data: payload.data || {}
|
|
81
|
-
},
|
|
82
|
-
trigger: null
|
|
83
78
|
});
|
|
84
79
|
|
|
80
|
+
return client;
|
|
85
81
|
}
|
|
86
82
|
|
|
87
83
|
/**
|
|
88
|
-
*
|
|
84
|
+
* Get token + subscribe
|
|
89
85
|
*/
|
|
90
86
|
export async function getToken(
|
|
91
87
|
appName: string,
|
|
92
88
|
brokerUrl: string = DEFAULT_BROKER
|
|
93
89
|
): Promise<string> {
|
|
94
90
|
|
|
95
|
-
if (!appName)
|
|
96
|
-
throw new Error("appName required");
|
|
97
|
-
}
|
|
91
|
+
if (!appName) throw new Error("appName required");
|
|
98
92
|
|
|
99
93
|
const token = await generateToken();
|
|
100
94
|
|
|
@@ -104,24 +98,7 @@ export async function getToken(
|
|
|
104
98
|
|
|
105
99
|
mqttClient.subscribe(topic);
|
|
106
100
|
|
|
107
|
-
console.log("Subscribed:", topic);
|
|
108
|
-
|
|
109
|
-
mqttClient.on("message", async (_, message) => {
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
|
|
113
|
-
const payload: NotificationPayload =
|
|
114
|
-
JSON.parse(message.toString());
|
|
115
|
-
|
|
116
|
-
await showNotification(payload);
|
|
117
|
-
|
|
118
|
-
} catch (err) {
|
|
119
|
-
|
|
120
|
-
console.error("Invalid notification payload", err);
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
});
|
|
101
|
+
console.log("📡 Subscribed:", topic);
|
|
125
102
|
|
|
126
103
|
return token;
|
|
127
104
|
}
|