在第一次启动应用之前,从通知到片段?

时间:2016-06-09 12:11:18

标签: android android-fragments push-notification google-cloud-messaging

我正在开发一个应用程序,当用户点击推送通知时,我想在MainActivity上发送片段(我称他为FragmentSRB)。当应用程序位于前台或后台时,一切正常。但是当我杀死应用程序,然后我收到通知时,通知只发送给主要活动,而不是FragmentSRB。我该怎么办?

这是myFirebaseMessagingService:

public class myFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG="MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.d(TAG, "From " + remoteMessage.getFrom());
    Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
    sendNotification(remoteMessage);
    Log.d("Msg", "Poruka je stigla");
}

private void sendNotification(RemoteMessage remoteMessage){
    Intent intent=new Intent(myFirebaseMessagingService.this, MainActivity.class);
    intent.putExtra("action", "goToFragmentSRB");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notification=new NotificationCompat.Builder(this)
            .setSmallIcon(logo)
            .setContentText(remoteMessage.getNotification().getBody())
            .setContentTitle("Naslov")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification.build());



}

我的主要活动:

public class MainActivity extends AppCompatActivity {
Button dugme, dugme2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("onCreate", "ONCREATE");
    setContentView(R.layout.activity_main);
    Intent intent=getIntent();
    String msg = getIntent().getStringExtra("action");
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    if (msg != null) {
        if (msg.equals("goToFragment1")) {
            Fragment1 fragment1 = new FragmentSRB();
            fragmentTransaction.replace(R.id.myFragment, fragmentSRB);
            Log.d("FragmentTransaction", "Fragment je promenjen u onCreate!");
            fragmentTransaction.commit();
            Log.d("Create", "Kraj onCreatea");
        }
    }

    dugme = (Button) findViewById(R.id.dugme1);
    dugme2 = (Button) findViewById(R.id.subscribe);
    dugme.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Fragment fragment = null;
            if (view == dugme) {
                fragment = new Fragment1();
            }
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.myFragment, fragment);
            transaction.addToBackStack(null);
            transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
            transaction.commit();
        }
    });


    dugme2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FirebaseMessaging.getInstance().subscribeToTopic("android");
            Log.d("Log", "Uspesno ste se pretplatili");
        }
    });

}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Log.d("onNewIntent", "NewIntent");
    String msg = getIntent().getStringExtra("action");
    Log.d("Msg", "msg");
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    if (msg != null) {
        if (msg.equals("goToFragment1")) {
            Fragment1 fragment1 = new FragmentSRB();
            fragmentTransaction.replace(R.id.myFragment, fragmentSRB);
            fragmentTransaction.commit();
        }
    }
}


@Override
protected void onResume() {
    super.onResume();
    Log.d("onResume", "Resume");
    Intent intent = new Intent();
    String msg = getIntent().getStringExtra("action");
    Log.d("msg", "msg");
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    if (msg != null) {
        if (msg.equals("goToFragment1")) {
            Fragment1 fragment1 = new FragmentSRB();
            fragmentTransaction.replace(R.id.myFragment, fragmentSRB);
            Log.d("FragmentTransaction", "Fragment je promenjen!");
            fragmentTransaction.commit();
            Log.d("onResume", "Kraj resuma");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

public class MainActivity extends AppCompatActivity {

    Button dugme, dugme2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("onCreate", "ONCREATE");
        setContentView(R.layout.activity_main);
        Intent intent = getIntent();
        handleIntentExtra(intent);

        dugme = (Button) findViewById(R.id.dugme1);
        dugme2 = (Button) findViewById(R.id.subscribe);
        dugme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment fragment = null;
                if (view == dugme) {
                    fragment = new Fragment1();
                }
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.myFragment, fragment);
                transaction.addToBackStack(null);
                transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
                transaction.commit();
            }
        });


        dugme2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FirebaseMessaging.getInstance().subscribeToTopic("android");
                Log.d("Log", "Uspesno ste se pretplatili");
            }
        });

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleIntentExtra(intent);
    }

    private void handleIntentExtra(Intent intent) {
        String msg = intent.getStringExtra("action");
        Log.d("Msg", "msg");
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (msg != null) {
            if (msg.equals("goToFragment1")) {
                Fragment1 fragment1 = new Fragment1();
                fragmentTransaction.replace(R.id.myFragment, fragment1);
                fragmentTransaction.commit();
            }
        }
    }}