短信发不出去

Text message not getting sent

我正在尝试将消息发送到多个号码。但是消息没有被发送。我已经添加了在服务中发送消息的代码,以确保即使应用程序在后台也能执行代码。

我还在清单中添加了权限并请求了运行时权限。

消息服务:

public class MessageService extends Service {

    ArrayList<String>  numbers = new ArrayList<>();
    private SharedPreferences sharedpreferences;
    private String mUserName;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        sharedpreferences = getSharedPreferences("UserProfile", Context.MODE_PRIVATE);

        mUserName = sharedpreferences.getString("UserUsername", "");

        numbers = intent.getStringArrayListExtra("numbers");

        for (String number:numbers) {

              sendMsg(number);

           // sendSMS("8655864341","Hello");
        }

        return super.onStartCommand(intent, flags, startId);
    }

    public void sendMsg(final String num){
        String SENT = "SMS_SENT";
        final SmsManager sms = SmsManager.getDefault();
        final PendingIntent sentPI = PendingIntent.getBroadcast(MessageService.this, 0,new Intent(SENT), 0);
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
            @Override
            public void run() {
              //  sms.sendTextMessage(num, null, "Hi,I am " + mUserName + "add me to your unique contact list and you never need to update" +
                      //  " any changes anymore! Click download below to download the App." + "https://play.google.com/apps/testing/com.weberz", sentPI, null);

                sms.sendTextMessage(num, null, "Hi,add me to your unique contact list and you never need to update" +
                        " any changes anymore! Click download below to download the App.", sentPI, null);
            }
        }, 3000);


    }

}

这东西以前工作过,收到了号码上的消息。现在我尝试在消息中添加 link 和用户名,但它们没有被发送。

然后我再次尝试从消息中删除 link 和用户名,并检查消息是否已发送并且现在无法正常工作。

我也想知道如何在短信中添加 hyperlinks 以便用户在单击 link 后可以打开网页?

如果过去收到过 SMS 消息,但在尝试发送超链接后它不起作用,可能是因为您使用的无线提供商已阻止您的 SIM 卡接收可疑的 SMS 流量。带有链接的邮件通常被视为垃圾邮件。

尝试更换 SIM 卡并再次发送 "Hello World" 消息。

SMS 中的超链接被 phone 识别,最聪明的 phone 使它们可以点击。所以,只需将 URL 放在短信中,收件人就可以点击它。它适用于 iPhone 和 Android phone 的大部分。

您可能因为您的 SMS 提供商而被阻止。因为他们通常会阻止 link 私人客户发送的邮件以避免垃圾邮件。

将 hyperlink 添加到 SMS 中,只需将 URL 正常插入即可 细绳。就像处理普通文本一样。大多数智能手机都会 解析 link 并使其成为 'clickable',即使它在 SMS 中也是如此。 此外考虑使用缩短 URL 的库,因为 短信限制为 160 个字符。这是我正在使用的库:https://github.com/vekexasia/android-googl-shortener-lib.

发送短信我推荐使用:

void sendSMS(String phoneNumber, String message) {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNumber, null, message, null, null);
        }

该方法使用了SmsManager,非常好用(API19)。想要查询更多的信息: https://developer.android.com/reference/android/telephony/SmsManager.html