2017-03-28 4 views
0

私はアンドロイドアプリに反応ネイティブを使用しています。私は、クリップボードイベントをリッスンするバックグラウンドサービスを持っています。アプリが興味のあるものがクリップボードにある場合、アプリはユーザーにプッシュ通知を送信します。アプリがバックグラウンドで実行されている場合、私はイベントを発行することができ、それは完全に動作していますが、アプリが閉じられて通知のタップで起動されても何も表示されません。反応文脈も空である。通知開始時にこのイベントをどのように発行できますか?通知タップからアプリを起動してイベントを発行するにはどうすればよいですか?

import android.app.Application; 
import android.content.Intent; 
import android.view.View; 
import android.util.Log; 
import android.os.Bundle; 
import android.app.NotificationManager; 
import android.support.v4.content.LocalBroadcastManager; 
import com.facebook.react.modules.core.DeviceEventManagerModule; 

import com.facebook.react.bridge.ReactContext; 
import com.facebook.react.ReactActivity; 
import com.facebook.react.common.LifecycleState; 
import com.facebook.react.ReactInstanceManager; 
import com.facebook.react.shell.MainReactPackage; 

public class MainActivity extends ReactActivity { 

    /** 
    * Returns the name of the main component registered from JavaScript. 
    * This is used to schedule rendering of the component. 
    */ 
    @Override 
    protected String getMainComponentName() { 
     return "PurchaseButton"; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     onNewIntent(getIntent()); 
    } 

    @Override 
    public void onNewIntent(Intent intent) { 
     setIntent(intent); 
     Bundle bundle = intent.getExtras(); 

     if (bundle != null) { 
      String result = bundle.getString("response"); 
      int nid = bundle.getInt("nid"); 
      NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
      mNotifyMgr.cancel(nid); 

      ReactContext ctx = getReactInstanceManager().getCurrentReactContext(); 
      if (ctx != null) { 
       ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("my.custom.event", result); 
       Log.d("PB", "sent broadcast.."); 
      } else { 
       Log.d("PB", "react context is null..."); 
      } 
     } 
    } 
} 
+0

あなたの活動はAndroidライフサイクルのためにまだ作成されていないと思います。あなたの活動がonResumeを実行するまで延期しようとしましたか? – Oximer

+0

@Oximer実際には、その部分をハンドラでonResumeに移動し、1秒間延期します。そのように、私は実際に反応のコンテキストを取得しました。しかし、発生したイベントに反応できるようにするには、5秒間設定しなければなりませんでした。 – Mengu

+0

今、それは機能していますか?私はそれを解決する簡単な方法を見ていない。オプションは、あなた自身のアクティビティを使用し、あなた自身で反応するネイティブインスタンスを作成することです。そのためにはReactInstanceManagerとReactInstanceEventListener.onReactContextInitializedを使用する必要があります – Oximer

答えて

0

誰でもこれについて不思議です。ここでは、反応したネイティブ0.42.0について回答した質問があります。

アンドロイドはactivity lifecyclesで、React Contextは、onCreateおよびonStartライフサイクルメソッドでは使用できませんが、onResumeです。私たちはリアクションコンテキストが必要です。なぜなら、これが私たちのネイティブフロントエンドにイベントを放出できるからです。したがって、イベントを正常に発行するには、ハンドラタスクを作成して5秒間遅延させ、実行するとイベントを発行する必要があります。私の場合、1秒も遅らせると文脈が得られますが、フロントエンドはイベントを待つ準備ができていません。ここにサンプルコードがあります:

package com.purchasebutton; 

import android.app.Application; 
import android.content.Intent; 
import android.view.View; 
import android.util.Log; 
import android.os.Bundle; 
import android.os.Handler; 
import android.app.NotificationManager; 
import android.support.v4.content.LocalBroadcastManager; 
import com.facebook.react.modules.core.DeviceEventManagerModule; 

import com.facebook.react.bridge.ReactContext; 
import com.facebook.react.ReactActivity; 
import com.facebook.react.common.LifecycleState; 
import com.facebook.react.ReactInstanceManager; 
import com.facebook.react.shell.MainReactPackage; 

public class MainActivity extends ReactActivity { 

    /** 
    * Returns the name of the main component registered from JavaScript. 
    * This is used to schedule rendering of the component. 
    */ 
    @Override 
    protected String getMainComponentName() { 
     return "PurchaseButton"; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     onNewIntent(getIntent()); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     final Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        ReactContext ctx = getReactInstanceManager().getCurrentReactContext(); 
        if (ctx != null) { 
         Intent intent = getIntent(); 
         Bundle bundle = intent.getExtras(); 
         if (bundle != null) { 
          String result = bundle.getString("response"); 
          int nid = bundle.getInt("nid"); 
          NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
          notificationManager.cancel(nid); 
          ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("product.changed", result); 
         } 
        } 
       } 
      }, 5000); 
    } 

    @Override 
    public void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 
     setIntent(intent); 
    } 
} 
関連する問題