├── .gitignore ├── LICENSE ├── README.md ├── index.android.js ├── index.ios.js ├── npm-debug.log ├── package.json └── platforms └── android └── AndroidManifest.xml /.gitignore: -------------------------------------------------------------------------------- 1 | demo/lib/ 2 | demo/platforms/ 3 | demo/node_modules -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2015 Ryan Lebel 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NativeScript Messenger 2 | 3 | A NativeScript module providing an sms messaging action for iOS and Android. 4 | 5 | ## Installation 6 | 7 | Run `tns plugin add nativescript-messenger` 8 | 9 | ## Usage 10 | 11 | To use the messenger module you must first `require()` it. 12 | 13 | ```js 14 | var messenger = require( "nativescript-messenger" ); 15 | ``` 16 | 17 | ### Method 18 | 19 | ####send: Sends a message to one or multiple recipients 20 | 21 | ##### Parameters 22 | * numbers: An array of phone number strings. Supports just one or multiple. 23 | * message: The body message. 24 | * subject: The subject message. 25 | 26 | ```js 27 | var messenger = require( "nativescript-messenger" ); 28 | var numbers = ["905-454-1234", "905-454-4321", "905-929-1122"]; 29 | messenger.send(numbers, "My message", "Subject of Message"); 30 | ``` 31 | 32 | This method also returns a promise. Use as so to define actions after the user has either canceled or sent the message. 33 | 34 | ```js 35 | messenger.send(["905-555-5555", "905-555-4444"], "this is body").then(function(args){ 36 | console.log(args.response); 37 | // either a string saying cancelled or sent 38 | console.log(args.message); 39 | // just a string with more detail than response. 40 | 41 | /* you can do stuff here.. this happens back 42 | in your app after the message window has 43 | been dismissed */ 44 | 45 | }, function (e) { 46 | console.log("Error occurred " + e); 47 | // e will show a vague error message. 48 | }); 49 | ``` -------------------------------------------------------------------------------- /index.android.js: -------------------------------------------------------------------------------- 1 | function send(numbers, message, subject) { 2 | return new Promise(function (resolve, reject) { 3 | if(Array.isArray(numbers)){ 4 | const numList = numbers.map((n,i)=>{ 5 | if(i === numbers.length - 1){ 6 | return n; 7 | }else{ 8 | return n+',' 9 | } 10 | }).join(""); 11 | try { 12 | var intent = new android.content.Intent(android.content.Intent.ACTION_VIEW); 13 | intent.setData(android.net.Uri.parse("smsto:" + numList)); 14 | intent.putExtra("sms_body", message); 15 | intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK); 16 | com.tns.NativeScriptApplication.getInstance().startActivity(intent); 17 | console.log("Message Sent."); 18 | resolve({ 19 | response: "sent", 20 | message: "Message sent." 21 | }); 22 | } catch(ex) { 23 | console.log("Something Failed."); 24 | reject(Error("Message send failed.")); 25 | } 26 | } else { 27 | console.log("Something Failed."); 28 | reject(Error("Message send failed.")); 29 | } 30 | 31 | }); 32 | } 33 | 34 | exports.send = send; 35 | -------------------------------------------------------------------------------- /index.ios.js: -------------------------------------------------------------------------------- 1 | var frameModule = require("ui/frame"); 2 | 3 | var CustomMessageCompositeViewControllerDelegate = NSObject.extend({ 4 | 5 | // Author: Иван Бухов 6 | // Author URL: https://github.com/ivanbuhov 7 | // pass resolve and reject functions to the delegate instance and save them as instance properties 8 | 9 | initWithResolveReject: function(resolve, reject) { 10 | var self = this.super.init(); 11 | if(self) { 12 | this.resolve = resolve; 13 | this.reject = reject; 14 | } 15 | return self; 16 | }, 17 | 18 | messageComposeViewControllerDidFinishWithResult: function(controller, result) { 19 | 20 | controller.dismissModalViewControllerAnimated(true); 21 | 22 | if(result == MessageComposeResultCancelled) { 23 | console.log("Message Cancelled."); 24 | this.resolve({ 25 | response: "canceled", 26 | message: "User cancelled the message." 27 | }); 28 | } 29 | else if(result == MessageComposeResultSent) { 30 | console.log("Message Sent."); 31 | this.resolve({ 32 | response: "sent", 33 | message: "Message sent." 34 | }); 35 | } 36 | else { 37 | console.log("Something Failed."); 38 | this.reject(Error("Message send failed.")); 39 | } 40 | 41 | // release the delegate instance 42 | CFRelease(controller.messageComposeDelegate); 43 | } 44 | }, { 45 | protocols: [MFMessageComposeViewControllerDelegate] 46 | }); 47 | 48 | function send(numbers, message, subject) { 49 | 50 | return new Promise(function (resolve, reject) { 51 | 52 | if(MFMessageComposeViewController.canSendText()){ 53 | var controller = MFMessageComposeViewController.alloc().init(); 54 | if(controller != null){ 55 | 56 | if(numbers && numbers.constructor === Array){ 57 | controller.recipients = numbers; 58 | } else { 59 | reject(Error("You must provide an array with number(s) as strings.")); 60 | } 61 | if(message){ 62 | controller.body = message; 63 | } 64 | if(subject){ 65 | controller.subject = subject; 66 | } 67 | 68 | var delegate = CustomMessageCompositeViewControllerDelegate.alloc().initWithResolveReject(resolve, reject); 69 | // retain the delegate because messageComposeDelegate property won't do it for us 70 | CFRetain(delegate); 71 | controller.messageComposeDelegate = delegate; 72 | var page = frameModule.topmost().ios.controller; 73 | page.presentModalViewControllerAnimated(controller, true); 74 | 75 | }else{ 76 | reject(Error("You're not able to send SMS messages. Please check device settings.")); 77 | } 78 | } else { 79 | reject(Error("You're not able to send SMS messages. Please check device settings.")); 80 | } 81 | 82 | }); 83 | } 84 | 85 | exports.send = send; 86 | -------------------------------------------------------------------------------- /npm-debug.log: -------------------------------------------------------------------------------- 1 | 0 info it worked if it ends with ok 2 | 1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'publish' ] 3 | 2 info using npm@2.14.7 4 | 3 info using node@v4.2.2 5 | 4 verbose publish [ '.' ] 6 | 5 verbose stack Error: Invalid version: "1.2" 7 | 5 verbose stack at Object.module.exports.fixVersionField (/usr/local/lib/node_modules/npm/node_modules/normalize-package-data/lib/fixer.js:186:13) 8 | 5 verbose stack at /usr/local/lib/node_modules/npm/node_modules/normalize-package-data/lib/normalize.js:32:38 9 | 5 verbose stack at Array.forEach (native) 10 | 5 verbose stack at normalize (/usr/local/lib/node_modules/npm/node_modules/normalize-package-data/lib/normalize.js:31:15) 11 | 5 verbose stack at final (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:338:5) 12 | 5 verbose stack at then (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:113:5) 13 | 5 verbose stack at /usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:300:12 14 | 5 verbose stack at /usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:76:16 15 | 5 verbose stack at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:380:3) 16 | 6 verbose cwd /Users/ryanlebel/AppPlugins/nativescript-messenger 17 | 7 error Darwin 15.6.0 18 | 8 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "publish" 19 | 9 error node v4.2.2 20 | 10 error npm v2.14.7 21 | 11 error Invalid version: "1.2" 22 | 12 error If you need help, you may report this error at: 23 | 12 error 24 | 13 verbose exit [ 1, true ] 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nativescript-messenger", 3 | "version": "1.2.0", 4 | "description": "A SMS Messenger NativeScript module for Android and iOS and Android", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/firescript/nativescript-messenger" 9 | }, 10 | "keywords": [ 11 | "NativeScript", 12 | "SMS", 13 | "Messenger", 14 | "iOS", 15 | "Android", 16 | "Text Messaging", 17 | "Group Message", 18 | "Group Messaging" 19 | ], 20 | "author": "Ryan Lebel ", 21 | "contributors": [ 22 | { 23 | "name": "Ivan Buhov", 24 | "url": "https://github.com/ivanbuhov" 25 | }, 26 | { 27 | "name": "Steve McNiven-Scott", 28 | "email": "steve@sitefinitysteve.com", 29 | "url": "https://github.com/sitefinitysteve" 30 | }, 31 | { 32 | "name": "Osei Fortune", 33 | "email": "fortune.osei@yahoo.com", 34 | "url": "https://github.com/triniwiz" 35 | } 36 | ], 37 | "license": { 38 | "type": "MIT", 39 | "url": "https://github.com/firescript/nativescript-messenger/blob/master/LICENSE" 40 | }, 41 | "bugs": { 42 | "url": "https://github.com/firescript/nativescript-messenger/issues" 43 | }, 44 | "homepage": "https://github.com/firescript/nativescript-messenger", 45 | "nativescript": {} 46 | } 47 | -------------------------------------------------------------------------------- /platforms/android/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | --------------------------------------------------------------------------------