单击通知而不是MainActivity时,应打开另一个活动

时间:2017-03-06 13:31:05

标签: android

我在firebase中收到通知但是当我点击通知时我正在获取MainActivity页面我希望获得Nextpage活动 我没有得到它可以任何人帮助请 这是下面的代码

MainActivity

package com.example.reema.firebase1;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.firebase.iid.FirebaseInstanceId;


public class MainActivity extends ActionBarActivity {
    private String TAG;

    //private static final String TAG = ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tkn = FirebaseInstanceId.getInstance().getToken();
                Toast.makeText(MainActivity.this, "Current token [" + tkn + "]",
                        Toast.LENGTH_LONG).show();
                Log.d("App", "Token [" + tkn + "]");

            }
        });
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

FireMsgService

package com.example.reema.firebase1;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class FireMsgService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        Log.d("Msg", "Message received [" + remoteMessage + "]");

        // Create Notification
//        Intent intent = new Intent(this, Nextpage.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//
//
//        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410,
//                intent, PendingIntent.FLAG_ONE_SHOT);
//
//        NotificationCompat.Builder notificationBuilder = new
//                NotificationCompat.Builder(this)
//                .setSmallIcon(R.drawable.firebase_icon)
//                .setContentTitle("Message")
//                .setContentText(remoteMessage.getNotification().getBody())
//                .setAutoCancel(true)
//                .setContentIntent(pendingIntent);
//
//        NotificationManager notificationManager =
//                (NotificationManager)
//                        getSystemService(Context.NOTIFICATION_SERVICE);
//
//        notificationManager.notify(1410, notificationBuilder.build());
      //  Context context;
        Context context = getApplicationContext();
        int icon = R.drawable.firebase_icon;
        CharSequence tickerText = "Call Blocker";
        CharSequence title = "Call Blocker";
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, tickerText, when);

        Intent notificationIntent = new Intent(context, Nextpage.class);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        NotificationCompat.Builder notificationBuilder = new
               NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.firebase_icon)
                .setContentTitle("Message")
                .setContentText(remoteMessage.getNotification().getBody())
                .setAutoCancel(true)
                .setContentIntent(intent);
        //notification.setLatestEventInfo(context, title, tickerText, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }
}

下一页

package com.example.reema.firebase1;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class Nextpage extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another);
        TextView textView = (TextView) findViewById(R.id.informationTextView);
       textView.setText( "You clicked the button {0} times in the previous activity.");


    }
}

1 个答案:

答案 0 :(得分:0)

原因是您正在创建NotificationBuilder,但实际上并未从中获取通知。改变你的代码:

    Context context = getApplicationContext();
    int icon = R.drawable.firebase_icon;
    CharSequence tickerText = "Call Blocker";
    CharSequence title = "Call Blocker";
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(context, Nextpage.class);

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    NotificationCompat.Builder notificationBuilder = new
           NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.firebase_icon)
            .setContentTitle("Message")
            .setContentText(remoteMessage.getNotification().getBody())
            .setAutoCancel(true)
            .setContentIntent(intent);
    Notification notification = notificationBuilder.build();
    //notification.setLatestEventInfo(context, title, tickerText, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);