【问题标题】:Sending 50+ messages causing issues lead to generic failure发送 50 多条导致问题的消息会导致一般故障
【发布时间】:2017-08-22 13:16:26
【问题描述】:

我正在开发一个应用程序,我需要在其中发送 100 多条消息。在经历了几个线程之后,我才知道发送消息是有限制的,比如一个小时可以发送 100 条消息。为此,我将我的收件人列表划分为多个块,并在每个块之间放置 5 秒的延迟,并在每条消息中放置 3 秒的延迟。每个块之后块之间的延迟都会增加,当它达到 100 秒时,它将重置为 5 秒。之后,它可以正常处理 50 条消息,但是当我提出收件人列表时,它会导致一些消息没有放在第一位,并在本机中显示为错误消息。

是否有任何标准方法可以实现这一点,我可能需要发送 100 多条消息,我怎样才能一次发送多条消息而不会失败。如果我需要延迟什么应该是块或消息之间的适当延迟。

提前致谢。

private final int MAX_SMS_IN_ONE_TIME = 10;
private final int DELAY_BETWEEN_CHUNKS = 5000;

public void sendMessage(arguments){ 
    // Send long messages in chunk of 20 messages and put gap of increasing 5 seconds till 50 seconds and then reset.

    final Iterator iterator = messageChunks.iterator();
    new Thread(new Runnable() {
        @Override
        public void run(){

        int interval =1;
        while (iterator.hasNext()) {

            for (final Contact contact :
                (List<Contact>) iterator.next()) {

                sendSMS(body, contact.getmMobileNumbers().get(0));

                App.trackEvent("Message", "Sent", "Messages from our sms app");
            }
        }
        try {
            Log.i("chunk", "chunk # " + interval + " delay is " + DELAY_BETWEEN_CHUNKS);
            Thread.sleep(DELAY_BETWEEN_CHUNKS * interval);
            interval++;
            if (interval == 10) {
                interval = 1;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        }
        }
    }).start();
}

public void sendSMS(final String message, final String phoneNo) {
    try {
        String SENT = "com.ebryx.smscustommessagegeneration"+""+System.currentTimeMillis()+""+((int)this.getmMessageId());
        Intent intentMessageASendStatus = new Intent(SENT);

        final PendingIntent pi = PendingIntent.getBroadcast(App.getContext(),  ((int)this.getmMessageId()),
                intentMessageASendStatus, PendingIntent.FLAG_CANCEL_CURRENT);
        final ArrayList<PendingIntent> sentPI = new ArrayList<PendingIntent>(){{add(pi);}};

        App.getContext().registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {


                switch (getResultCode())
                {
                    case Activity.RESULT_OK:

                        Log.i("tag","sent successfully ");
            break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

                        Log.i("tag","Generic Failure");
                break;

                    case SmsManager.RESULT_ERROR_NO_SERVICE:

                        Log.i("tag","No service failure");
            break;

        case SmsManager.RESULT_ERROR_NULL_PDU:
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:

                        Log.i("tag","Airplane mode failure");
                        break;
                }
            }
        }, new IntentFilter(SENT));

        final SmsManager smsManager = SmsManager.getDefault();
        final ArrayList<String> parts = smsManager.divideMessage(message);

        new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    smsManager.sendMultipartTextMessage(phoneNo, null, parts, sentPI, null);

                }}, 3000);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

【问题讨论】:

  • 您看到的错误信息是什么?
  • @ScottNewson,我没有看到任何错误消息,但由于一般故障,很少有消息没有发送。

标签: android sms smsmanager


【解决方案1】:

对于 SENT 和 DELIVER,您应该使用两个具有未决意图的广播接收器。

消息传递后,您必须添加回调机制以通知消息已传递并统计发送新消息。此调用应同步

  1. 创建一个HashMap,并根据我们在下面的delivery广播接收器中得到的delivery状态,将其一一发送到下面的方法中。

        /**
         * Sends an SMS message to another device
         * 
         * @param phoneNumber Number to which msg send
         * @param message Text message
         */
    private void sendSMS(String phoneNumber, String message) {
        // Intent Filter Tags for SMS SEND and DELIVER
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";
    // STEP-1___
        // SEND PendingIntent
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
                SENT), 0);
    
        // DELIVER PendingIntent
        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                new Intent(DELIVERED), 0);
    // STEP-2___
        // SEND BroadcastReceiver
        BroadcastReceiver sendSMS = new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off",                            Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        };
    
        // DELIVERY BroadcastReceiver
        BroadcastReceiver deliverSMS = new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                     // TODO : notify from here to send new message.
                     // Add callback mechanism 
                    Toast.makeText(getBaseContext(), "SMS delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        };
    // STEP-3___
        // ---Notify when the SMS has been sent---
        registerReceiver(sendSMS, new IntentFilter(SENT));
    
        // ---Notify when the SMS has been delivered---
        registerReceiver(deliverSMS, new IntentFilter(DELIVERED));
    
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
     }
    }
    
  2. 您还可以从您的 HashMap 中删除元素作为您的 SMS DELIVERED 这可能有助于您跟踪成功传递的消息数量。

【讨论】:

  • 感谢您的回答@jitesh 感谢它。但我在另一个答案中读到“At & T 手机不支持消息传递报告”。我在 Lg g3 和 Samsung S5 上测试了两部未调用的手机交付广播接收器。所以把业务逻辑放在上面是有风险的。
  • 在调查它时,我发现有预定义的及时发送消息限制,例如每小时 100 条消息和每半小时 30 条消息。为避免这种情况,您需要 root 手机并更改该限制。阅读后,我认为发送失败的消息可能是其原因,如果我错了,我的限制是正确的。请分享你的想法。链接如下:stackoverflow.com/questions/19079151/…
【解决方案2】:

似乎没有关于 SMS 发送限制的官方文档,从我可以找到的内容和以下站点中也可以找到。

Unfortunately 似乎没有来自 谷歌的 Android 开发团队在这个问题上。

这是从 2017 年 5 月 17 日开始

我能找到的关于短信限制的唯一数字来自Commonsware 的网站:

SMS Sending Limitations
在 Android 1.x 和 2.x 设备上运行的应用程序限制为每小时发送 100 条 SMS 消息,在此之前 用户开始收到每个 SMS 消息请求的提示以确认 他们确实希望发送它。

在 Android 4.x 设备上运行的应用,现在限制为 30 条短信 30 分钟内的消息.../...

似乎没有办法提高该限制without rooting 电话。您需要更改以下设置的地方。以下将允许每 180000 毫秒 == 30 分钟发送 1000 条短信。

SMS_OUTGOING_CHECK_MAX_COUNT 1000
SMS_OUTGOING_CHECK_INTERVAL_MS 1800000

常见的(令人沮丧的)Android 问题也适用于不同设备的性能差异。一部手机的性能可能与另一部不同。

这家公司已经确定了某些手机及其产品的最大 SMS 容量。 SMS sending limits with FrontlineSync and Android。他们还建议可能需要对手机进行 Root 以增加限制。

相关资源:

Check android package SMS limit?

is there a limit to the number of numbers to send an SMS?

Android Java: How To Send SMS & Receive SMS & Get SMS Messages 日期为 2017 年 8 月 30 日。

【讨论】:

    猜你喜欢
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    相关资源
    最近更新 更多