Skip to content

Commit 69a7ca2

Browse files
committed
Step 14.6: Added ionic-native plugin for reading SMS OTP
1 parent b7c2e05 commit 69a7ca2

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

src/ionic/sms-receiver/index.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {Injectable} from '@angular/core';
2+
import {Cordova, Plugin, IonicNativePlugin} from '@ionic-native/core';
3+
4+
5+
/**
6+
* @name SmsReceiver
7+
* @description
8+
* Allows you to receive incoming SMS. You have the possibility to start and stop the message broadcasting.
9+
*
10+
* Requires Cordova plugin: `cordova-plugin-smsreceiver`. For more info, please see the [Cordova SmsReceiver docs](https://github.com/ahmedwahba/cordova-plugin-smsreceiver).
11+
*
12+
* @usage
13+
* ```typescript
14+
* import { SmsReceiver } from '@ionic-native/smsreceiver';
15+
*
16+
*
17+
* constructor(private smsReceiver: SmsReceiver) { }
18+
*
19+
* ...
20+
*
21+
* this.smsReceiver.isSupported().then(
22+
* (supported) => console.log('Permission granted'),
23+
* (err) => console.log('Permission denied: ', err)
24+
* );
25+
*
26+
* this.smsReceiver.startReceiving().then(
27+
* (msg) => console.log('Message received: ', msg)
28+
* );
29+
*
30+
* this.smsReceiver.stopReceiving().then(
31+
* () => console.log('Stopped receiving'),
32+
* (err) => console.log('Error: ', err)
33+
* );
34+
* ```
35+
*/
36+
@Plugin({
37+
pluginName: 'SmsReceiver',
38+
plugin: 'cordova-plugin-smsreceiver',
39+
pluginRef: 'sms',
40+
repo: 'https://github.com/ahmedwahba/cordova-plugin-smsreceiver',
41+
platforms: ['Android']
42+
})
43+
@Injectable()
44+
export class SmsReceiver extends IonicNativePlugin {
45+
/**
46+
* Check if the SMS permission is granted and SMS technology is supported by the device.
47+
* In case of Marshmallow devices, it requests permission from user.
48+
* @returns {void}
49+
*/
50+
@Cordova()
51+
isSupported(callback: (supported: boolean) => void, error: () => void): void {
52+
return;
53+
}
54+
55+
/**
56+
* Start the SMS receiver waiting for incoming message.
57+
* The success callback function will be called every time a new message is received.
58+
* The error callback is called if an error occurs.
59+
* @returns {void}
60+
*/
61+
@Cordova({
62+
platforms: ['Android']
63+
})
64+
startReceiving(callback: (msg: string) => void, error: () => void): void {
65+
return;
66+
}
67+
68+
/**
69+
* Stop the SMS receiver.
70+
* @returns {void}
71+
*/
72+
@Cordova({
73+
platforms: ['Android']
74+
})
75+
stopReceiving(callback: () => void, error: () => void): void {
76+
return;
77+
}
78+
}

0 commit comments

Comments
 (0)