Search code examples
ionic2cordova-plugins

Sending SMS with cordova-sms-plugin hangs


I used cordova-sms-plugin 0.1.11 "Cordova SMS Plugin" for my ionic2 app to send text message. I just wanted to simply open the default SMS app on user's device. Below is my code:

public sendSMS(phoneNumber){
        var option = {
            replaceLineBreaks: false,
            android: {
                intent: 'INTENT'
            },
            ios: {
                intent: 'INTENT'
            }
        };
        SMS.send(phoneNumber, "", option)
            .then(()=>{
                //this.showAlert("Message sent!", "");
                alert("Message sent");
            }, (err)=>{
                console.log("sms fail:"+err);
            })
    }

The code works as expected to open the SMS app on user's device. However, on the opened SMS, my users need to tap on the recipient to make the "soft keyboard" to show up and users can no longer use the functions the sms provided (like attaching photos).

Plus, users could only send one message, and the sms app closed itself and "redirect" back to my app.

If users quit my app and open their SMS app independently, the SMS app works fine again.

I don't know what I did wrong on my code. Or is there any better cordova plugin I can use that fulfills my purpose?

Thank you for your help in advance :-)


Solution

  • This cordova plugin (cordova-sms-plugin) is better to be used if you want to send text messages from the "backend". e.g. Design your own text message box, send button...

    If you just want to open the default SMS app use the code below:

    import { DomSanitizer } from '@angular/platform-browser';
    

    then add the service to constructor:

    constructor(private sanitizer:DomSanitizer) 
    

    Then the method to call:

    public sanitize(url: string):any {
        return this.sanitizer.bypassSecurityTrustUrl(url);
    }
    

    the method: return type use any. Some said use SafeUrl, but somehow the new service doesn't have that.

    Html view:

    <a [href]="sanitize('sms:' + item.sms)">