react native android 关照栏

react native android 关照栏的完成

近来介入部门用react native实践的第一个项目靠近尾声,在项目中rn的Android、iOS代码复用使我愈来愈感觉到rn开辟的上风,rn连系native的开辟要领多是未来疾速开辟APP的主流趋向。

近来工作中碰到一个题目就是关照栏的完成,看了下rn并没有直接申明完成关照栏的要领,加上我个人之前也没搞过native的开辟,所以一向没有思绪,厥后连系rn原生模块的挪用和Android开辟文档(需科学上网)给完成了,至于iOS的完成要领看文档说是须要开辟者账号,就先放放…

详细完成流程

1、建立js挪用的react模块,该步骤参考react native文档

//NotificationModule.js
public class NotificationModule extends ReactContextBaseJavaModule {
    public NotificationModule(ReactApplicationContext reactContext) {
        super(reactContext);
    @Override
    public String getName() {
        return "NotificationModule";
    }
    @ReactMethod
    public void startNotification(String title,String content){
        try{
            Intent intent = new Intent(getCurrentActivity(),NotificationService.class);
            intent.putExtra("title",title);
            intent.putExtra("content",content);
            getCurrentActivity().startService(intent);
        }catch(Exception e){
            throw new JSApplicationIllegalArgumentException(
                    "error "+e.getMessage());
        }
    }
}
//NotificationPackage.java
public class NotificationPackage implements ReactPackage {
    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        List<NativeModule> modules = new ArrayList<>();
        modules.add(new NotificationModule(reactContext));
        return modules;
    }
    @Override
    public List<Class<? extends JavaScriptModule>> createJSModules() {
        return Collections.emptyList();
    }
    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }
}

并在MainApplication.java中到场该新建的模块。

2、建立天生关照栏的IntentService类,在该类中挪用关照栏天生要领,详细参考谷歌的安卓开辟文档

public class NotificationService extends IntentService {
    public NotificationService() {
        super("NotificationService");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        String title = intent.getStringExtra("title");
        String content = intent.getStringExtra("content");
        System.out.println(title+content);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
       .setSmallIcon(R.drawable.directionscar)
                        .setContentTitle(title)
                        .setContentText(content);
        Intent resultIntent = new Intent(this, MainActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
         PendingIntent.FLAG_UPDATE_CURRENT
                );
mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
        stopSelf();
    }
}

须要注重的是:1、service天生关照后必需运用stopSelf()住手效劳
;2、只管运用Android studio自动天生service,会自动在manifest中举行设置。

详细完成思绪就是经由过程js挪用原生要领,在原生要领里建立intent,并经由过程intent挪用service天生体系关照栏。

思绪照样比较简单,主如果之前没有打仗过native开辟,所以一开始不清楚怎么做。经由过程rn疾速天生APP页面,并经由过程与原生开辟相连系的体式格局,可能会迎来普遍的运用。react native android 关照栏的完成就是如许,迎接交换讨论…

    原文作者:zorroyz
    原文地址: https://segmentfault.com/a/1190000008664749
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞