mbnotify-app 1.0.0 → 1.0.1
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 +0 -14
- package/dist/cjs/index.js +21 -39
- package/dist/esm/index.d.ts +0 -14
- package/dist/esm/index.js +21 -39
- package/package.json +6 -4
- package/src/index.ts +27 -61
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,16 +1,2 @@
|
|
|
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
|
-
/**
|
|
10
|
-
* Request notification permission (Expo)
|
|
11
|
-
*/
|
|
12
1
|
export declare function requestPermission(): Promise<boolean>;
|
|
13
|
-
/**
|
|
14
|
-
* Subscribe to notifications and return token
|
|
15
|
-
*/
|
|
16
2
|
export declare function getToken(appName: string, brokerUrl?: string): Promise<string>;
|
package/dist/cjs/index.js
CHANGED
|
@@ -2,21 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.requestPermission = requestPermission;
|
|
4
4
|
exports.getToken = getToken;
|
|
5
|
-
const mqtt_1 = require("mqtt");
|
|
5
|
+
const mqtt_1 = 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 notification permission (Expo)
|
|
12
|
-
*/
|
|
13
10
|
async function requestPermission() {
|
|
14
11
|
const { status } = await Notifications.requestPermissionsAsync();
|
|
15
12
|
return status === "granted";
|
|
16
13
|
}
|
|
17
|
-
/**
|
|
18
|
-
* Generate or retrieve device token
|
|
19
|
-
*/
|
|
20
14
|
async function generateToken() {
|
|
21
15
|
let token = await async_storage_1.default.getItem("mbnotify_token");
|
|
22
16
|
if (!token) {
|
|
@@ -28,9 +22,6 @@ async function generateToken() {
|
|
|
28
22
|
}
|
|
29
23
|
return token;
|
|
30
24
|
}
|
|
31
|
-
/**
|
|
32
|
-
* Connect MQTT broker
|
|
33
|
-
*/
|
|
34
25
|
function connectMQTT(brokerUrl) {
|
|
35
26
|
if (client)
|
|
36
27
|
return client;
|
|
@@ -39,46 +30,37 @@ function connectMQTT(brokerUrl) {
|
|
|
39
30
|
connectTimeout: 10000
|
|
40
31
|
});
|
|
41
32
|
client.on("connect", () => {
|
|
42
|
-
console.log("mbnotify app connected");
|
|
33
|
+
// console.log("✅ mbnotify app connected");
|
|
43
34
|
});
|
|
44
35
|
client.on("error", (err) => {
|
|
45
36
|
console.error("MQTT error:", err);
|
|
46
37
|
});
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
38
|
+
// ✅ SINGLE message listener
|
|
39
|
+
client.on("message", async (_, message) => {
|
|
40
|
+
try {
|
|
41
|
+
const payload = JSON.parse(message.toString());
|
|
42
|
+
await Notifications.scheduleNotificationAsync({
|
|
43
|
+
content: {
|
|
44
|
+
title: payload.title || "",
|
|
45
|
+
body: payload.body || "",
|
|
46
|
+
data: payload.data || {}
|
|
47
|
+
},
|
|
48
|
+
trigger: null
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
console.error("Invalid payload", err);
|
|
53
|
+
}
|
|
60
54
|
});
|
|
55
|
+
return client;
|
|
61
56
|
}
|
|
62
|
-
/**
|
|
63
|
-
* Subscribe to notifications and return token
|
|
64
|
-
*/
|
|
65
57
|
async function getToken(appName, brokerUrl = DEFAULT_BROKER) {
|
|
66
|
-
if (!appName)
|
|
58
|
+
if (!appName)
|
|
67
59
|
throw new Error("appName required");
|
|
68
|
-
}
|
|
69
60
|
const token = await generateToken();
|
|
70
61
|
const topic = `/${appName}/${token}/notification`;
|
|
71
62
|
const mqttClient = connectMQTT(brokerUrl);
|
|
72
63
|
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
|
-
});
|
|
64
|
+
// console.log("📡 Subscribed:", topic);
|
|
83
65
|
return token;
|
|
84
66
|
}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,16 +1,2 @@
|
|
|
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
|
-
/**
|
|
10
|
-
* Request notification permission (Expo)
|
|
11
|
-
*/
|
|
12
1
|
export declare function requestPermission(): Promise<boolean>;
|
|
13
|
-
/**
|
|
14
|
-
* Subscribe to notifications and return token
|
|
15
|
-
*/
|
|
16
2
|
export declare function getToken(appName: string, brokerUrl?: string): Promise<string>;
|
package/dist/esm/index.js
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
|
-
import mqtt from "mqtt";
|
|
1
|
+
import 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 notification permission (Expo)
|
|
8
|
-
*/
|
|
9
6
|
export async function requestPermission() {
|
|
10
7
|
const { status } = await Notifications.requestPermissionsAsync();
|
|
11
8
|
return status === "granted";
|
|
12
9
|
}
|
|
13
|
-
/**
|
|
14
|
-
* Generate or retrieve device token
|
|
15
|
-
*/
|
|
16
10
|
async function generateToken() {
|
|
17
11
|
let token = await AsyncStorage.getItem("mbnotify_token");
|
|
18
12
|
if (!token) {
|
|
@@ -24,9 +18,6 @@ async function generateToken() {
|
|
|
24
18
|
}
|
|
25
19
|
return token;
|
|
26
20
|
}
|
|
27
|
-
/**
|
|
28
|
-
* Connect MQTT broker
|
|
29
|
-
*/
|
|
30
21
|
function connectMQTT(brokerUrl) {
|
|
31
22
|
if (client)
|
|
32
23
|
return client;
|
|
@@ -35,46 +26,37 @@ function connectMQTT(brokerUrl) {
|
|
|
35
26
|
connectTimeout: 10000
|
|
36
27
|
});
|
|
37
28
|
client.on("connect", () => {
|
|
38
|
-
console.log("mbnotify app connected");
|
|
29
|
+
// console.log("✅ mbnotify app connected");
|
|
39
30
|
});
|
|
40
31
|
client.on("error", (err) => {
|
|
41
32
|
console.error("MQTT error:", err);
|
|
42
33
|
});
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
34
|
+
// ✅ SINGLE message listener
|
|
35
|
+
client.on("message", async (_, message) => {
|
|
36
|
+
try {
|
|
37
|
+
const payload = JSON.parse(message.toString());
|
|
38
|
+
await Notifications.scheduleNotificationAsync({
|
|
39
|
+
content: {
|
|
40
|
+
title: payload.title || "",
|
|
41
|
+
body: payload.body || "",
|
|
42
|
+
data: payload.data || {}
|
|
43
|
+
},
|
|
44
|
+
trigger: null
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
catch (err) {
|
|
48
|
+
console.error("Invalid payload", err);
|
|
49
|
+
}
|
|
56
50
|
});
|
|
51
|
+
return client;
|
|
57
52
|
}
|
|
58
|
-
/**
|
|
59
|
-
* Subscribe to notifications and return token
|
|
60
|
-
*/
|
|
61
53
|
export async function getToken(appName, brokerUrl = DEFAULT_BROKER) {
|
|
62
|
-
if (!appName)
|
|
54
|
+
if (!appName)
|
|
63
55
|
throw new Error("appName required");
|
|
64
|
-
}
|
|
65
56
|
const token = await generateToken();
|
|
66
57
|
const topic = `/${appName}/${token}/notification`;
|
|
67
58
|
const mqttClient = connectMQTT(brokerUrl);
|
|
68
59
|
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
|
-
});
|
|
60
|
+
// console.log("📡 Subscribed:", topic);
|
|
79
61
|
return token;
|
|
80
62
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mbnotify-app",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
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,23 +1,11 @@
|
|
|
1
|
-
import mqtt from "mqtt";
|
|
1
|
+
import 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:
|
|
7
|
+
let client: any = null;
|
|
8
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
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Request notification permission (Expo)
|
|
20
|
-
*/
|
|
21
9
|
export async function requestPermission(): Promise<boolean> {
|
|
22
10
|
|
|
23
11
|
const { status } = await Notifications.requestPermissionsAsync();
|
|
@@ -25,9 +13,6 @@ export async function requestPermission(): Promise<boolean> {
|
|
|
25
13
|
return status === "granted";
|
|
26
14
|
}
|
|
27
15
|
|
|
28
|
-
/**
|
|
29
|
-
* Generate or retrieve device token
|
|
30
|
-
*/
|
|
31
16
|
async function generateToken(): Promise<string> {
|
|
32
17
|
|
|
33
18
|
let token = await AsyncStorage.getItem("mbnotify_token");
|
|
@@ -45,10 +30,7 @@ async function generateToken(): Promise<string> {
|
|
|
45
30
|
return token;
|
|
46
31
|
}
|
|
47
32
|
|
|
48
|
-
|
|
49
|
-
* Connect MQTT broker
|
|
50
|
-
*/
|
|
51
|
-
function connectMQTT(brokerUrl: string): mqtt.MqttClient {
|
|
33
|
+
function connectMQTT(brokerUrl: string) {
|
|
52
34
|
|
|
53
35
|
if (client) return client;
|
|
54
36
|
|
|
@@ -58,43 +40,44 @@ function connectMQTT(brokerUrl: string): mqtt.MqttClient {
|
|
|
58
40
|
});
|
|
59
41
|
|
|
60
42
|
client.on("connect", () => {
|
|
61
|
-
console.log("mbnotify app connected");
|
|
43
|
+
// console.log("✅ mbnotify app connected");
|
|
62
44
|
});
|
|
63
45
|
|
|
64
|
-
client.on("error", (err) => {
|
|
46
|
+
client.on("error", (err: any) => {
|
|
65
47
|
console.error("MQTT error:", err);
|
|
66
48
|
});
|
|
67
49
|
|
|
68
|
-
|
|
69
|
-
|
|
50
|
+
// ✅ SINGLE message listener
|
|
51
|
+
client.on("message", async (_: any, message: any) => {
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
|
|
55
|
+
const payload = JSON.parse(message.toString());
|
|
56
|
+
|
|
57
|
+
await Notifications.scheduleNotificationAsync({
|
|
58
|
+
content: {
|
|
59
|
+
title: payload.title || "",
|
|
60
|
+
body: payload.body || "",
|
|
61
|
+
data: payload.data || {}
|
|
62
|
+
},
|
|
63
|
+
trigger: null
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
} catch (err) {
|
|
67
|
+
console.error("Invalid payload", err);
|
|
68
|
+
}
|
|
70
69
|
|
|
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
70
|
});
|
|
84
71
|
|
|
72
|
+
return client;
|
|
85
73
|
}
|
|
86
74
|
|
|
87
|
-
/**
|
|
88
|
-
* Subscribe to notifications and return token
|
|
89
|
-
*/
|
|
90
75
|
export async function getToken(
|
|
91
76
|
appName: string,
|
|
92
77
|
brokerUrl: string = DEFAULT_BROKER
|
|
93
78
|
): Promise<string> {
|
|
94
79
|
|
|
95
|
-
if (!appName)
|
|
96
|
-
throw new Error("appName required");
|
|
97
|
-
}
|
|
80
|
+
if (!appName) throw new Error("appName required");
|
|
98
81
|
|
|
99
82
|
const token = await generateToken();
|
|
100
83
|
|
|
@@ -104,24 +87,7 @@ export async function getToken(
|
|
|
104
87
|
|
|
105
88
|
mqttClient.subscribe(topic);
|
|
106
89
|
|
|
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
|
-
});
|
|
90
|
+
// console.log("📡 Subscribed:", topic);
|
|
125
91
|
|
|
126
92
|
return token;
|
|
127
93
|
}
|