0

서버 측의 데이터로 알림을 보내고 선택한 데이터를 해당 활동으로 표시하려고합니다. 지금까지 나는 서버에서 알림을 보내고 내가 선택한 모든 활동에이를 표시했습니다. 내가하지 않는 무엇Firebase가 서버 API의 키 쌍 값으로 알림을 전송합니다.

알림 데이터를 포함하는 방법

  1. 입니까?
  2. 활동에서 어떻게 받습니까?

필자는 가이드를 따라했지만 Firebase 콘솔의 데이터로 알림 만 보냈습니다.

<?php 
    require "init.php"; 
    $message =$_POST['message']; 
    $title=$_POST['title']; 
//firebase server url 
    $path_to_fcm='https://fcm.googleapis.com/fcm/send'; 
    // Here is the server_key 
    $server_key="#########"; 
    $token="#########"; 
    $headers= array(
    'Authorization:key='.$server_key, 
    'Content-Type:application/json' 
      ); 
       // here use $tokens as the replace $key 
$fields= array('to'=>$token, 
'notification'=>array('title'=>$title,'body'=>$message, 
    'click_action'=>'com.fredycom.tundevufirebasemysql_TARGET_NOTIFICATION' 
)); 
    $payload= json_encode($fields); 
    $curl_session= curl_init(); 
    curl_setopt($curl_session,CURLOPT_URL,$path_to_fcm); 
    curl_setopt($curl_session,CURLOPT_POST,true); 
    curl_setopt($curl_session,CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($curl_session,CURLOPT_RETURNTRANSFER,true); 
    curl_setopt($curl_session,CURLOPT_SSL_VERIFYPEER,false); 
    curl_setopt($curl_session,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4); 
    curl_setopt($curl_session,CURLOPT_POSTFIELDS,$payload); 
     $result =curl_exec($curl_session);  
     curl_close($curl_session); 
    mysqli_close($con); 
    ?> 

// PHP 코드에서 활동

public class MainActivity extends AppCompatActivity { 
    TextView title, message; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

     // REGISTER BROADCAT RECEIVER 
     LocalBroadcastManager.getInstance(this).registerReceiver(mHandler, new IntentFilter("com.fredycom.myreartime_FCM_MESSAGE")); 
    setContentView(R.layout.activity_main); 
    title = (TextView) findViewById(R.id.title_content); 
    message = (TextView) findViewById(R.id.message_content); 

    if (getIntent().getExtras() != null) 
    { 
     for (String key : getIntent().getExtras().keySet()) { 
      if (key.equals("title")) 
      { 
       title.setText(getIntent().getExtras().getString(key)); 
      } 
      else if (key.equals("message")) 
      { 
       message.setText(getIntent().getExtras().getString(key)); 
      } 
      } 
      } 
      } 
     // HERE IS THE BROADICAST RECEIVER GET DATA FROM SMS RECIVING SERVICE 
     private BroadcastReceiver mHandler= new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String Mytitle= intent.getStringExtra("title"); 
     title.setText(Mytitle); 
     String Mymessage= intent.getStringExtra("message"); 
     message.setText(Mymessage); 
     } 
     }; 

    protected void onPause(){ 
     super.onPause(); 
     LocalBroadcastManager.getInstance(this).unregisterReceiver(mHandler); 
    } 
} 

답변

1

에 메시지 서비스

public void onMessageReceived(RemoteMessage remoteMessage) { 
    // HANDLE THE FIREBASE NOTIFICATION DATA RECEIVED WHEN APP RUN IN THE FOREGROUND 

    if(remoteMessage.getData().size()>0){ 
     String title= remoteMessage.getData().get("title"); 
     String message= remoteMessage.getData().get("message"); 
     // now we send the data to the main activity 
     Intent intent= new Intent("com.fredycom.myreartime_FCM_MESSAGE"); 
     intent.putExtra("title",title); 
     intent.putExtra("message",message); 
     LocalBroadcastManager localBroadcastManager= LocalBroadcastManager.getInstance(this); 
     localBroadcastManager.sendBroadcast(intent); 
    } 

가 // 처리 메시지가 나타납니다, 당신은

이 // 서버 측에 대한

$fields= array('to'=>$token, 
'notification'=>array('title'=>$title,'body'=>$message, 
    'click_action'=>'com.fredycom.tundevufirebasemysql_TARGET_NOTIFICATION' 
)); 

이이 같은 페이로드 될 것입니다 : 한 notification - 단지 페이로드를 보내는

{ 
    "to": "#######", 
    "notification" : { 
    "title": "title", 
    "body" : "message", 
    "click_action" : "com.fredycom.tundevufirebasemysql_TARGET_NOTIFICATION" 
    } 
} 

. # 1에 답하기 위해 알림에 데이터를 추가하려면 notification 매개 변수와 마찬가지로 data 매개 변수를 사용하면됩니다. 그래서 같이 :

$datamsg = array 
(
    'key' => 'value', 
    'key2' => 'value2', 
    'key3' => 'value3', 
); 

그런 다음 단순히 $fields에 추가 :

# 2의
$fields = array 
(
    'registration_ids' => $registrationIds, 
    'data'   => $datamsg 
); 

, 당신은 이미 data 메시지를받을 수있는 코드를 가지고 있지만, 위에서 언급 한 바와 같이, 당신은 notification을 보내고 있었다 - 전용 메시지이므로 을 (를) (으)로 캐치 할 수 없습니다. 그냥 당신이 올바른 키를 지정하고 있는지 확인하십시오 : 당신은 단순히 notification 메시지에서 정보를 얻을 수 있었으면한다면, 당신은 RemoteMessage.getNotification()를 사용해야합니다

String value = remoteMessage.getData().get("key"); 
String value2 = remoteMessage.getData().get("key2"); 
String value3 = remoteMessage.getData().get("key3"); 

. 그런 다음 원하는 활동으로 데이터를 보냅니다. 페이로드에 따라 보낸 메시지를 처리하는 방법을 이해했는지 확인하십시오.

자세한 내용은 Handling Messages in Android을 참조하십시오.

+0

이 엄청난 설명과 함께 내 인생을 구하기 위해 sooo 많은 @AL 감사합니다 .. 계속 ... –

+0

당신은 오신 것을 환영합니다. 앱을 사용해보세요. :) –