2016-10-19 12 views
0

キーベースのコンソールを使用してfirebaseコンソールから通知を送信し、ランチャーアクティビティで通知を処理しました。以下は試したコードです:アプリがバックグラウンドのときに通知をクリックしたときのターゲットアクティビティを開く方法

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    if (intent.hasExtra("click_action")) { 
     ClickActionHelper ck=new ClickActionHelper(); 
     ck.startActivity(intent.getStringExtra("click_action"), intent.getExtras(), this); 
    } 
} 



public class ClickActionHelper { 
    public void startActivity(String className, Bundle extras, Context context){ 
     Class cls=null; 
     try { 
      cls = Class.forName(className); 
     }catch(ClassNotFoundException e){ 

     } 
     Intent i = new Intent(context, cls); 
     i.putExtras(extras); 
     context.startActivity(i); 
    } 
} 

このようにして、通知のクリックでターゲットのアクティビティを開くことができません。何か案は?

+1

Read https://developer.android.com/training/notify-user/navigation.html – Raghunandan

+0

@Raghunandan私の質問は異なっています。私は特定のアクティビティを開始する必要があり、それはapp.soのすべてのアクティビティになることができます上記のリンクに記載されているすべての属性をすべてのアクティビティで定義し、フラグメントをロードする必要がある場合はどうすればよいですか? – chandra

+0

オープンする各アクティビティに関連するタイプがあります。それに基づいて私はスイッチケースを使用して、その特定の活動にナビゲートする。フラグメントはアクティビティによってホストされます。だから、その場合フラグメントを開始する必要があります – Raghunandan

答えて

1

アクティビティをナビゲートする必要がある場合は、実装に欠けているNotification class(NotificationCompat.Builder)のバインドが必要です。

NotificationCompat.Builder builder = new **NotificationCompat.Builder(this); builder.setContentIntent(resultPendingIntent);** 
ここ resultPendingIntent

以下のように通知して活動のためbackstackを含むにするために使用されます:行の下

はどんな活動リダイレクトすることが非常に重要である

を//含むPendingIntentを取得します。全体のバックスタックPendingIntent

resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

完全なコードスニペットはhereです。

int id = 1; 

Intent resultIntent = new Intent(this, ResultActivity.class); 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
// Adds the back stack 
stackBuilder.addParentStack(ResultActivity.class); 
// Adds the Intent to the top of the stack 
stackBuilder.addNextIntent(resultIntent); 
// Gets a PendingIntent containing the entire back stack 
PendingIntent resultPendingIntent = 
     stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
builder.setContentIntent(resultPendingIntent); 
NotificationManager mNotificationManager = 
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
mNotificationManager.notify(id, builder.build()); 

編集:

  • 私はそれが
  • はあなたが

    { "registration_ids" 以下のようにサーバからの応答を設定していると仮定働く方法を説明しましょう:[ "XXX "、...]、 " data ":{ " id_offer ":" 41 " }、 "通知":{ "タイトル": "これはタイトルである"、 "テキスト": "こんにちは、私は通知だ"、 "アイコン": "ic_push"、 "click_action": "ACTIVITY_XPTO" }}

そして、あなたのマニフェストファイル内

<activity android:name=".ActivityXPTO" android:screenOrientation="sensor" android:windowSoftInputMode="stateHidden"> <intent-filter> <action android:name="ACTIVITY_XPTO" /> 
<category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> 

アプリを閉じたり、バックグラウンドで、ユーザーは012を取得するために、それは私のActivityXPTOを開き、通知をクリックした場合は私だけ実行する必要があります。

public class ActivityXPTO extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 
     String idOffer = ""; 
     Intent startingIntent = getIntent(); 
     if (startingIntent != null) { 
      idOffer = startingIntent.getStringExtra("id_offer"); 
     // Retrieve the id 
     } 
     getOfferDetails(id_offer); 
     } 
    } 
} 

を、以下のようなデータペイロードを介して第2ウェイ

  • 発呼キーでとgetIntent経由MainActivityでキーを取得()および特異的活性またはそのフラグメントを呼び出します。

    json1.put( "title"、 "Your Title");

    json1.put( "body"、 "body content");

    json1。put( "メッセージ"、 "あなたのメッセージ");

    json1.put( "screen"、 "2"); // secondFragmentは、ナビゲーションドロワーの2番目の位置にあります。

    json.put( "data"、json1);

GitHubのサンプルプロジェクト。

+0

上記のコードによると、ユーザーがResultActivity.classを開くと通知が表示されます。私のコードでは、キーの値にクラス名を送信しようとしています:click_action from firebase console。私はfirebaseコンソールで定義されているその特定のクラスを開いてみたい。それは可能ですか? – chandra

+1

"編集"セクションの答えを編集しました。親切にそれを通過します。それはあなたに役立つかどうか私に教えてください。 –

+1

すばらしい答え:) Thanks Patrick – chandra

関連する問題