2017-03-16 15 views
0

私はアプリケーションを作成しようとしていますが、私が応答を得ている間、私はサーバーreqと応答を得ています。だから私はこれをすべてサービスで作ったのですが、レスポンスを得た後、私はブロードキャストレシーバのサーバーレスポンスからの情報をアクティビティで表示して、recyclerview.infoに情報を表示しています(通知はないが、私は直接放送をしましたが、私はrecyclerviewに何も表示されない通知をクリックしています。この問題を解決する方法..通知で情報が表示されないandroid

NotificationActivity:

public class NotificationActivity extends AppCompatActivity { 

    GetPostAsyncTask backgroundTask; 
    ArrayList<NotificationDataClass> list; 
    NotificationListShowAdapter notificationListShowAdapter; 
    RecyclerView recyclerView; 
    Context contextNotificationActivity; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_notification); 

     Intent intent=new Intent(NotificationActivity.this,ServiceClass.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startService(intent); 
     contextNotificationActivity=NotificationActivity.this; 

     recyclerView=(RecyclerView)findViewById(R.id.recyclerViewId); 

     /*notificationListShowAdapter=new NotificationListShowAdapter(contextNotificationActivity,list); 
     LinearLayoutManager layoutManager=new LinearLayoutManager(contextNotificationActivity); 
     recyclerView.setLayoutManager(layoutManager); 
     recyclerView.setAdapter(notificationListShowAdapter); 
     DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(contextNotificationActivity, 
       layoutManager.getOrientation()); 
     recyclerView.addItemDecoration(dividerItemDecoration); 
     notificationListShowAdapter.notifyDataSetChanged();*/ 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(broadcastReceiver,new IntentFilter("Notification")); 
    } 

    private BroadcastReceiver broadcastReceiver=new BroadcastReceiver() { 


     @Override 
     public void onReceive(Context context, Intent intent) { 

      Toast.makeText(context,"Receiver",Toast.LENGTH_LONG).show(); 

      String result=intent.getStringExtra("Result"); 
      Log.v("Brd",intent.getStringExtra("Result")); 
      JSONObject jsonObject= null; 
      list=new ArrayList<>(); 
      try { 

       jsonObject = new JSONObject(result); 
//    JSONObject codeObj=jsonObject.optJSONObject("code"); 
       JSONArray jsonArray=jsonObject.getJSONArray("payload"); 


       for(int i=0;i<jsonArray.length();i++){ 
        JSONObject jsonObject1=new JSONObject(jsonArray.optString(i)); 


        NotificationDataClass notificationDataClass=new NotificationDataClass(); 
        notificationDataClass.setName(jsonObject1.getString("Name")); 
        notificationDataClass.setRemarks(jsonObject1.getString("Remarks")); 
        Log.v("hsdgfjgsdh", String.valueOf(jsonObject1.getInt("Type"))); 
        if(jsonObject1.getInt("Type")==1){ 
         notificationDataClass.setInout("In"); 
        }else { 
         notificationDataClass.setInout("Out"); 
        } 

        notificationDataClass.setTime(jsonObject1.getString("DateTime")); 


        list.add(i,notificationDataClass); 

       } 

       notificationListShowAdapter=new NotificationListShowAdapter(context,list); 
       LinearLayoutManager layoutManager=new LinearLayoutManager(context); 
       recyclerView.setLayoutManager(layoutManager); 
       recyclerView.setAdapter(notificationListShowAdapter); 
       DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context, 
         layoutManager.getOrientation()); 
       recyclerView.addItemDecoration(dividerItemDecoration); 
       notificationListShowAdapter.notifyDataSetChanged(); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }; 


    @Override 
    protected void onPause() { 
     super.onPause(); 
     LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(broadcastReceiver); 
    } 
} 

serviceClass:

public class ServiceClass extends Service { 

    GetPostAsyncTask_ServiceClass backgroundTask; 
    Context context; 
    Handler handler; 
    Runnable runnable; 
    LocalBroadcastManager localBroadcastManager; 


    int iny=1; 


    @Override 
    public void onCreate() { 
     super.onCreate(); 
     localBroadcastManager=LocalBroadcastManager.getInstance(this); 
    } 

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

     context=getApplicationContext(); 

     handler=new Handler(); 

     runnable=new Runnable() { 
      @Override 
      public void run() { 

       String queryForNotification=null; 


       try { 
        queryForNotification = URLEncoder.encode("dateTime", "UTF-8") + "=" + URLEncoder.encode("03/13/2017", "UTF-8"); 

        Log.v("INRTR","out"+iny++); 

        backgroundTask=new GetPostAsyncTask_ServiceClass(context); 

        backgroundTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"GET","GetHrOnDutyActivityByDate?"+queryForNotification,""); 

       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 

       } 

//    ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(1000); 

       handler.postDelayed(runnable,10000); 
      } 
     }; 

     handler.postDelayed(runnable,10000); 

     return START_STICKY; 
    } 


    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 





    //Asynctask 
    private class GetPostAsyncTask_ServiceClass extends AsyncTask<String, Void, String> { 

//  public AsyncResult asyncResult; 
     ProgressDialog progressDialog; 
     private final String baseUrl = "http://192.168.0.6:880/MobileAPI/"; 


     Context context; 


     GetPostAsyncTask_ServiceClass(Context context) { 

      this.context = context; 
//   this.asyncResult = asyncResult; 
     } 

     @Override 
     protected void onPreExecute() { 
      //Toast.makeText(context,"Loading..",Toast.LENGTH_SHORT).show(); 
      progressDialog = new ProgressDialog(context); 
//  progressDialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... args) { 

      try { 
       // setting the URL 
       URL url = new URL(baseUrl + args[1]); 
       HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
       httpURLConnection.addRequestProperty("Apiuser", "SDLPayrollAPI"); 
       httpURLConnection.addRequestProperty("Apikey", "DFGHJ*UH45445^TY"); 

       // setting the method type 
       httpURLConnection.setRequestMethod(args[0]); 
       httpURLConnection.setDoInput(true); 
       httpURLConnection.setDoOutput(true); 
       OutputStream outputStream = httpURLConnection.getOutputStream(); 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); 

       // setting the identification key name with query params 
       bufferedWriter.write(args[2]); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 

       httpURLConnection.connect(); 

       String line = ""; 
       String res = ""; 

       // prepare the output buffer 
       InputStream inputStream = httpURLConnection.getInputStream(); 
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); 

       while ((line = bufferedReader.readLine()) != null) { 

        res += line; 

       } 

       inputStream.close(); 

       httpURLConnection.disconnect(); 

       return res.toString(); 

      } catch (IOException e) { 
       Log.v("PesDSS","doBCa"+e.toString()); 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 
      super.onProgressUpdate(values); 
     } 

     @Override 
     protected void onPostExecute(String result) { 

      super.onPostExecute(result); 
      Log.v("PesDSS","out"+result); 



      Intent intent1; 
//   progressDialog.dismiss(); 
      if (result != null) { 
//    asyncResult.asyncResult(result); 
       Log.v("PesDSS","in"+result); 

       //notification 

       NotificationCompat.Builder notification=new NotificationCompat.Builder(context) 
       .setSmallIcon(R.drawable.systech_logo) 
       .setContentTitle("Notification") 
       .setContentText("Delivered") 
       .setAutoCancel(true) 
       ; 

       intent1 = new Intent(context, NotificationActivity.class); 
       intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       intent1.setAction("Notification"); 

       intent1.putExtra("Result",result); 
       LocalBroadcastManager.getInstance(context).sendBroadcast(intent1); 


       TaskStackBuilder taskStackBuilder=TaskStackBuilder.create(context); 
       taskStackBuilder.addParentStack(NotificationActivity.class); 
       taskStackBuilder.addNextIntent(intent1); 

       PendingIntent pendingIntent=taskStackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); 
//    PendingIntent pendingIntent=PendingIntent.getBroadcast(context,0,intent1,PendingIntent.FLAG_CANCEL_CURRENT); 

       notification.setContentIntent(pendingIntent); 

       NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
       notificationManager.notify(0,notification.build()); 
      } 
     } 
    } 
} 

通知データクラス:私はスタックオーバーフローが、私でしからソリューションを追跡しようとした

public class NotificationDataClass { 

    String name; 
    String remarks; 
    String inout; 
    String time; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getRemarks() { 
     return remarks; 
    } 

    public void setRemarks(String remarks) { 
     this.remarks = remarks; 
    } 

    public String getInout() { 
     return inout; 
    } 

    public void setInout(String inout) { 
     this.inout = inout; 
    } 

    public String getTime() { 
     return time; 
    } 

    public void setTime(String time) { 
     this.time = time; 
    } 
} 

それを解決しないでください。どんな助けもありがとう!

答えて

1

FYI onReceive()は、現在のアクティビティがフォアグラウンドまたは可視の場合にのみ呼び出されます。通知のクリックでタスクを実行するために、あなたの活動/アプリがバックグラウンドで動作しているとき、あなたはまた、あなたがこのようなonReceive()で行っているonCreate()で同じことを行う必要があります。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_notification); 
     Intent notificationIntent = getIntent.getStringExtra("Result"); 
     if(notificationIntent != null){ 
      //do whatever you have done in onReceive(). 
     } 
} 
+0

を私のアプリが殺された場合に何をすべきか? @Megha MInar – AAA

+0

答えは大丈夫です。 –

+0

'FirebaseMessagingService'または' GCMIntentService'のどちらかに拡張された通知を扱うクラスを作ったことがありますか? –

関連する問題