无论如何,Android和Amp都会在后台发送短信iOS? [英] Is there anyway to send sms in background for both Android & IOS?

查看:72
本文介绍了无论如何,Android和Amp都会在后台发送短信iOS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个应用程序,其主要功能取决于发送短信.在我开发android(native)之前,但现在我使用React-Native使其同时适用于IOS和Android.

I wanna make an app that its main functionality depends on sending SMS. before i was developing android(native) but now im using React-Native to make it for both IOS and Android.

在android中,如果您获得用户许可,则可以在后台发送短信.但是通过网络搜索后,我找不到在本机中执行此操作的方法.我读到Apple不允许应用程式这样做.我想知道是否有针对Android和IOS的后台发送SMS.

in android it's possible to send sms in background if you get user permission. but after searching through net i couldn't find a way to do it in react-native. i read that Apple doesn't let apps to do so. i'm wondering if there is anyway to send SMS in background for both Android and IOS.

我已经看过的库,打开手机短信界面(用数字和smsBody填充),然后用户必须按发送"按钮(我要删除此步骤.我的意思是应用程序会自动发送短信).

the libraries i've already seen, open phone sms interface(filled with number and smsBody) and then user must push send button(i wanna remove this step. i mean app sends sms automatically).

毕竟,无论如何(库,...)可以在不打开android和ios的sms界面的情况下在后台发送短信?

after all, is there anyway( library, ...) that can sends sms in background without opening sms interface for both android and ios?

推荐答案

在android上,这是可能的.我也在自己的项目中实施.请按照给定的步骤进行操作:

On android, it is possible. I also implement in my own project. Follow the given step:

1.创建DirectSmsModule

在您的 android->中app->src/main->在MainActivity.java旁边的java/com/yourProject 中,创建一个扩展 ReactContextBaseJavaModule 类的Java类.

In your android-> app-> src/main-> java/com/yourProject next to the MainActivity.java create a java class that extends the ReactContextBaseJavaModule class.

//DirectSmsModule.java
package com.security_notifier;
 
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.uimanager.IllegalViewOperationException;
import android.telephony.SmsManager;

 
public class DirectSmsModule extends ReactContextBaseJavaModule {
 
    public DirectSmsModule(ReactApplicationContext reactContext) {
        super(reactContext); //required by React Native
    }
 
    @Override
    //getName is required to define the name of the module represented in JavaScript
    public String getName() {
        return "DirectSms";
    }
 
    @ReactMethod
    public void sendDirectSms(String numbers, String msg) {
        try {    
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(numbers, null, msg, null, null);   
        } catch (Exception ex) {
            System.out.println("couldn't send message.");
        }
    }
}

2.注册DirectSmsModule

对于注册,您应该创建一个实现ReactPackage的新包.在DirectSmsModule旁边创建一个DirectSmsPackage:

For the registration, you should create a new package that implements ReactPackage. Next to the DirectSmsModule create a DirectSmsPackage:

//DirectSmsPackage.java
package com.security_notifier;
 
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.security_notifier.DirectSmsModule;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
public class DirectSmsPackage implements ReactPackage {
 
    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }

    @Override
    public List<NativeModule> createNativeModules(
            ReactApplicationContext reactContext) {
        List<NativeModule> modules = new ArrayList<>();
        //this is where you register the module
        modules.add(new DirectSmsModule(reactContext));
        return modules;
    }
}

3.注册DirectSmsPackage

您需要正确注册新软件包.打开MainApplication.java并按如下所示对其进行修改:

You need to properly register the new package. Open the MainApplication.java and modify it as follow:

package com.security_notifier;

import android.app.Application;
import android.util.Log;

import com.facebook.react.PackageList;
import com.facebook.hermes.reactexecutor.HermesExecutorFactory;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.ReactApplication;
import com.tkporter.sendsms.SendSMSPackage;
// import com.tkporter.sendsms.SendSMSPackage;
import com.oblador.vectoricons.VectorIconsPackage;
import com.reactnativecommunity.netinfo.NetInfoPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;

import java.util.List;


import com.facebook.react.shell.MainReactPackage;

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      @SuppressWarnings("UnnecessaryLocalVariable")
      List<ReactPackage> packages = new PackageList(this).getPackages();
      // Packages that cannot be autolinked yet can be added manually here, for example:
      // packages.add(new MyReactNativePackage());

      packages.add(new DirectSmsPackage());

      return packages;
    }

    @Override
    protected String getJSMainModuleName() {
      return "index";
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);

  }
}

4.从javascript调用sendDirectSms方法

在您的javascript文件中:

In your javascript file:

import React from 'react';
    import {NativeModules} from 'react-native';
    var DirectSms = NativeModules.DirectSms;

export default class App extends React.Component {
       sendDirectSms() {
          DirectSms.sendDirectSms('0935...', 'This is a direct message');
      }
      render() {
        return (
                <View>
                    <TouchableOpacity onPress={() => this.sendDirectSms()}>
                        <Text>send SMS</Text>
                    </TouchableOpacity>
                </View>
               );
          }
}

5.设置用户权限

  • 对于API级别< 23

  • For API level<23

向manifest.xml添加权限:

Add permission to your manifest.xml:

<uses-permission android:name="android.permission.SEND_SMS"/>

  • 对于API 23及更高版本
  • 如果设备运行的是Android 6.0(API级别23)或更高版本,并且应用程序的targetSdkVersion为23或更高版本,则在安装时不会通知用户任何应用程序权限.您的应用程序必须要求用户在运行时授予危险权限.当您的应用请求权限时,用户会看到一个系统对话框,告诉用户您的应用正在尝试访问哪个权限组.该对话框包括拒绝"和允许"按钮.我们在javascript文件中处理权限运行时请求:

    If the device is running Android 6.0 (API level 23) or higher, and the app’s targetSdkVersion is 23 or higher, the user isn't notified of any app permissions at install time. Your app must ask the user to grant the dangerous permissions at runtime. When your app requests permission, the user sees a system dialog telling the user which permission group your app is trying to access. The dialog includes a Deny and Allow button. We handle the permission runtime request in our javascript file:

    async sendDirectSms() {
        try {
            const granted = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.SEND_SMS,
                {
                    title: 'YourProject App Sms Permission',
                    message:
                    'YourProject App needs access to your inbox ' +
                    'so you can send messages in background.',
                    buttonNeutral: 'Ask Me Later',
                    buttonNegative: 'Cancel',
                    buttonPositive: 'OK',
                },
            );
            if (granted === PermissionsAndroid.RESULTS.GRANTED) {
                DirectSms.sendDirectSms('0935...', 'This is a direct message');
            } else {
                console.log('SMS permission denied');
            }
        } catch (err) {
            console.warn(err);
        }
    }
    

    完成后,运行react-native run-android.

    Done, run react-native run-android.

    参考: https://blog.usejournal.com/sending-direct-sms-in-react-native-android-d902d6bf1f04

    在iOS上,无需用户交互即可发送SMS的唯一方法是使用外部提供商(例如Twilio);但该消息将来自您服务器的号码,而不是来自用户.@ Paulw11已在您的问题中发表评论

    On iOS, the only way you can send an SMS without user interaction is to use an external provider such as Twilio; but the message will come from your server's number, not from the user. @Paulw11 already commented in your question

    这篇关于无论如何,Android和Amp都会在后台发送短信iOS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆