为什么用户无法通过推送通知转到片段?

时间:2016-06-10 14:04:38

标签: android android-fragments push-notification

Hy,我正在开发一个使用推送通知的应用程序。当我点击通知时,该操作应该每次都将我发送到notificationFragment(当应用程序处于前台时,何时处于后台以及何时应用程序被杀并且在第一次启动应用程序之前发出通知)。这是我的代码,但它不起作用。

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");
}

我的HelperClass(扩展了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());
}

我的清单文件:

 <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>

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

而不是PendingIntent尝试这个

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

然后在您创建活动时

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