简体   繁体   中英

Why user can't go to the Fragment from push-notification?

Hy, I am developing an app which is using push notifications. When I click on notification, that action should sends me to the notificationFragment every time (when app is in foreground, when is in background and when app is killed and notification comes before first time launching app). This is my code, but it doesn't work.

MainActivity:

public class MainActivity extends AppCompatActivity {
Button dugme, dugme2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = getIntent();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    String msg = getIntent().getStringExtra("action");

    if (msg != null) {
        if (msg.equals("goToFragment1")) {
            NotificationFragmentClass fragment1 = new NotificationFragmentClass();
            fragmentTransaction.replace(R.id.myFragment, fragment1);
            Log.d("FragmentTransaction", "Fragment je promenjen u onCreate!");
            fragmentTransaction.commit();
            Log.d("Create", "Kraj onCreatea");
        }
    }

    dugme = (Button) findViewById(R.id.notfrag);
    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 NotificationFragmentClass();
            }
            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 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();
    Log.d("FragmentTransaction", "FragmentTransaction success!");
    if (msg != null)
    {
        if (msg.equals("goToFragment1")) {
            NotificationFragmentClass fragment1 = new NotificationFragmentClass();
            fragmentTransaction.replace(R.id.myFragment, fragment1);
            Log.d("FragmentTransaction", "Fragment je promenjen!");
            fragmentTransaction.commit();
            Log.d("onResume", "Kraj resuma");
        }
    }
}
@Override
protected void onPause() {
    super.onPause();  // Always call the superclass method first

    Log.d("onPause", "Pauza");
}

And my HelperClass(extends FirebaseMessagingService):

public class HelperClass  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(HelperClass.this, MainActivity.class);
    intent.putExtra("action", "goToFragment1");
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    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(R.mipmap.ic_launcher)
            .setContentText(remoteMessage.getNotification().getBody())
            .setContentTitle("Naslov")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification.build());
}

My manifest file:

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".HelperClass">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
</application>

Can anyone help me?

Instead of PendingIntent try this

Intent itent =
            new Intent(context, MainActivity.class);
resultIntent.putExtra("action", 1); //search map

then when you create the activity

int msg = getIntent().getIntExtra("action", 0)
if (msg != 0) {
   // opened from notification
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM