0

我正在製作一個使用Firebase通知和一切的應用程序,除了事實上用戶點擊通知,我想打開一個活動以及當用戶單擊後退按鈕時,想要返回主要活動,但現在應用僅在用戶點擊後退按鈕時關閉。Firebase通知單擊操作活動父活動

這裏是我的清單代碼:

<?xml version="1.0" encoding="utf-8"?> 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 

    package="com.example.massa.luxvilla"> 



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

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

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

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



    <application 

     android:allowBackup="true" 

     android:icon="@mipmap/ic_launcher" 

     android:label="@string/app_name" 

     android:supportsRtl="true" 

     android:theme="@style/AppTheme" 

     android:name=".LuxVilla"> 

     <meta-data 

      android:name="android.app.default_searchable" 

      android:value="com.example.massa.luxvilla.searchableactivity" /> 



     <provider 

      android:name=".sugestoes.SearchSugestionsProvider" 

      android:authorities="com.example.massa.luxvilla.sugestoes.SearchSugestionsProvider" /> 



     <activity android:name=".MainActivity" 

      android:theme="@style/Main"> 

      <intent-filter> 

       <action android:name="android.intent.action.MAIN" /> 



       <category android:name="android.intent.category.LAUNCHER" /> 

      </intent-filter> 

     </activity> 

     <activity android:name=".searchableactivity"> 

      <intent-filter> 

       <action android:name="android.intent.action.SEARCH" /> 

      </intent-filter> 



      <meta-data 

       android:name="android.app.searchable" 

       android:resource="@xml/searchable" /> 

     </activity> 

     <activity 

      android:name=".Actividades.casaactivity" 

      android:parentActivityName="com.example.massa.luxvilla.MainActivity" 

      android:theme="@style/Infocasa"> 

      <intent-filter> 

       <action android:name="casaactivity" /> 

       <category android:name="android.intent.category.DEFAULT" /> 

      </intent-filter> 



      <meta-data 

       android:name="android.support.PARENT_ACTIVITY" 

       android:value="com.example.massa.luxvilla.MainActivity"/> 

     </activity> 



     <activity android:name=".Actividades.SettingsActivity"></activity> 



     <service 



      android:name=".notificacoes.FirebaseMessagingService"> 



      <intent-filter> 



       <action android:name="com.google.firebase.MESSAGING_EVENT"/> 



      </intent-filter> 



     </service> 



     <service 



      android:name=".notificacoes.FirebaseInstanceIDService"> 



      <intent-filter> 



       <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 



      </intent-filter> 



     </service> 



     <meta-data 

      android:name="com.google.firebase.messaging.default_notification_color" 

      android:resource="@color/colorAccent" /> 

    </application> 



</manifest> 

和我的PHP腳本,發送通知:

<?php 

#API access key from Google API's Console 

    define('API_ACCESS_KEY', 'API_KEY'); 

    $topic = "/topics/todos"; 

$url = 'https://fcm.googleapis.com/fcm/send'; 
$fields = array (
     'to' => $topic, 
     'notification' => array (
       "body" => "nova casa adicionada", 
       "title" => "Nova casa", 
       "sound"=> "default", 
       "click_action"=> "casaactivity" 
     ), 
     'data' => array(
      "localcasa" => "local", 
      "precocasa"=> "Preço", 
      "imgurl"=> "link imagem", 
      "infocs"=> "info", 
      "csid"=> "id da casa" 
     ) 
); 
$fields = json_encode ($fields); 
$headers = array (
     'Authorization: key=' . API_ACCESS_KEY, 
     'Content-Type: application/json' 
); 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_POST, true); 
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, $fields); 

$result = curl_exec ($ch); 
curl_close ($ch); 

?> 

感謝。

+0

的是你要打開的活動的名稱? – Manza

+0

我想在待處理意圖中執行父堆棧的相同操作,打開casaactivity,並且當用戶單擊後退按鈕返回主活動 –

回答

0

你可以嘗試這樣的事情在Activity您通過通知開口:

@Override 
public void onBackPressed() { 
    if (isTaskRoot()) { 
     finish(); 
     startActivity(new Intent(this, MainActivity.class)); 
    } else { 
     super.onBackPressed(); 
    } 
} 

你會檢查,如果有Activity是在同一個任務的堆棧,如果沒有,你會打開MainActivity

+0

謝謝我沒有時間測試您的答案,因爲我有時間測試並查看結果。 –