【问题标题】:SMS Phonegap 3.0 plugin for Android and iOS适用于 Android 和 iOS 的 SMS Phonegap 3.0 插件
【发布时间】:2013-09-17 05:49:52
【问题描述】:

我正在寻找适用于 Phonegap 3.x 的 phonegap 插件。我需要它在 Android 和 iOS 上工作。如果两者都有 1 个插件会更好,但如果有 2 个我可以使用的单独插件也没关系。如果我可以使用以下命令安装它也更好:

phonegap local plugin add

有这样的插件吗?或者是否有关于如何升级现有短信插件以使用 phonegap 3.0 的说明?

编辑

我创建了一个可在 2.9 上运行的插件的 repo,我正试图让它在 phonegap 3.x 中运行 (https://github.com/aharris88/phonegap-sms-plugin) 到目前为止,我可以使用命令将其拉入我的 phonegap 项目

phonegap local plugin add https://github.com/aharris88/phonegap-sms-plugin

它正确地将所需的权限放入 AndroidManifest.xml 并将功能放入 res/xml/config.xml,但是当我将它安装在手机上时,它并没有说它需要发送文本的权限,而且我没有从这段代码中得到任何成功或错误消息:

var number = $('#number').val();
var message = $('#text').val();
alert("Send text to "+number+" with message: "+message);
SmsPlugin.prototype.send(number, message, '',
    function () {
        alert('Message sent successfully');
    },
    function (e) {
        alert('Message Failed:' + e);
    }
);

【问题讨论】:

    标签: cordova phonegap-plugins


    【解决方案1】:

    调试它的最佳方法是使用 ADT(Android 开发者工具)。有很多小错误。这篇文章是一个非常有用的资源:@​​987654321@

    这里是 sms.java 代码:

    package com.adamwadeharris.sms;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.telephony.SmsManager;
    import org.apache.cordova.CallbackContext;
    import org.apache.cordova.CordovaPlugin;
    import org.apache.cordova.PluginResult;
    
    public class Sms extends CordovaPlugin {
        public final String ACTION_SEND_SMS = "send";
    
        @Override
        public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
            if (action.equals(ACTION_SEND_SMS)) {
                try {               
                    String phoneNumber = args.getString(0);
                    String message = args.getString(1);
                    String method = args.getString(2);
    
                    if(method.equalsIgnoreCase("INTENT")){
                        invokeSMSIntent(phoneNumber, message);
                        callbackContext.sendPluginResult(new PluginResult( PluginResult.Status.NO_RESULT));
                    } else{
                        send(phoneNumber, message);
                    }
    
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
                    return true;
                }
                catch (JSONException ex) {
                    callbackContext.sendPluginResult(new PluginResult( PluginResult.Status.JSON_EXCEPTION));
                }           
            }
            return false;
        }
    
        private void invokeSMSIntent(String phoneNumber, String message) {
            Intent sendIntent = new Intent(Intent.ACTION_VIEW);
            sendIntent.putExtra("sms_body", message);
            sendIntent.putExtra("address", phoneNumber);
            sendIntent.setType("vnd.android-dir/mms-sms");
            this.cordova.getActivity().startActivity(sendIntent);
        }
    
        private void send(String phoneNumber, String message) {
            SmsManager manager = SmsManager.getDefault();
            PendingIntent sentIntent = PendingIntent.getActivity(this.cordova.getActivity(), 0, new Intent(), 0);
            manager.sendTextMessage(phoneNumber, null, message, sentIntent, null);
        }
    }
    

    这里是 sms.js 代码:

    var sms = {
        send: function(phone, message, method, successCallback, failureCallback) {
            cordova.exec(
                successCallback,
                failureCallback,
                'Sms',
                'send',
                [phone, message, method]
            );
        }
    }
    
    module.exports = sms;
    

    这里是plugin.xml:

    <plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
               id="com.adamwadeharris.sms"
          version="0.1.0">
        <name>Sms</name>
        <description>Cordova SMS Send Plugin</description>
        <license>MIT</license>
        <keywords>cordova,phonegap,sms</keywords>
    
    
        <js-module src="www/sms.js" name="Sms">
            <clobbers target="window.sms" />
        </js-module>
    
        <!-- android -->
        <platform name="android">
            <config-file target="res/xml/config.xml" parent="/*">
                <feature name="Sms">
                    <param name="android-package" value="com.adamwadeharris.sms.Sms"/>
                </feature>
            </config-file>
    
            <config-file target="AndroidManifest.xml" parent="/manifest">
                <uses-permission android:name="android.permission.SEND_SMS" />
            </config-file>
    
            <source-file src="src/android/Sms.java" target-dir="src/com/adamwadeharris/sms" />
        </platform>
    
    </plugin>
    

    编辑 另外,我在 github 上有可用的插件:https://github.com/aharris88/phonegap-sms-plugin

    编辑 该插件现已移至: https://github.com/cordova-sms/cordova-sms-plugin

    【讨论】:

    • 你在用我写的插件吗?您可能应该在那里提交一个更详细的问题,或者在 stackoverflow 上提出一个新问题。
    • 我已按照上述步骤操作。我想将意图保持为空白,所以我使用了 var intent = ""; sms.send(号码、消息、意图、成功、错误);我收到警报点击,但我没有收到任何响应消息,而且消息也没有发送。你能告诉我问题的原因吗
    • 我正在使用带有cordova 3.5版的android平台
    • 您可能只想在 github 上使用我的插件,因为自从这个答案以来,代码已经发生了一些变化。如果您有任何问题,请随时在 github 上提出问题:github.com/aharris88/phonegap-sms-plugin
    • 那是完全相同的链接。请在 github 上提出问题或在 stackoverflow 上创建您自己的问题。
    【解决方案2】:

    您可以查看以下在 android 中发送 SMS 的链接。 Git 上可用的所有其他 PhoneGap 插件都已过时。

    https://github.com/javatechig/phonegap-sms-plugin

    您可以找到上述插件here的使用步骤

    ** 我不确定它是否适用于 PhoneGap3.0,但在 2.9 版本中这对我来说非常有用。**

    【讨论】:

    • 我试过使用这个插件。问题是Phonegap 3.0 在处理插件方面发生了很大变化。我不确定如何使它与 phonegap 3.0 一起使用。
    • 我按照此页面上的说明进行操作,但收到错误“找不到类”
    • 我分叉了你的仓库,我正在尝试升级到 phonegap 3.0。让我知道你是否有任何让它发挥作用的想法。谢谢。
    • 我可以用这个插件发送短信,但它不会触发成功或错误回调
    • 对我来说也是如此...继续忽略 iOS 中的回调 ..在 droid 上工作,
    【解决方案3】:

    我最近刚刚将一个旧插件转换为 3.0,我对此有疑问,但完整的代码与我的问题的答案一起包含在内,这可能对您有所帮助。见下文

    Phonegap 3.0 Custom Plugin

    这个返回一个值给你的 javascript 调用者。希望对您有所帮助,我很乐意回答任何问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-31
      • 2020-01-11
      • 1970-01-01
      • 2013-08-26
      • 2013-01-28
      相关资源
      最近更新 更多