繁体   English   中英

为什么用户不能从推送通知转到片段?

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

嗨,我正在开发一个使用推送通知的应用程序。 当我单击通知时,该操作应每次将我发送至notificationFragment(当应用程序处于前台状态,何时处于后台状态以及何时应用程序被终止并且通知在首次启动应用程序之前出现)。 这是我的代码,但是不起作用。

主要活动:

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>

谁能帮我?

代替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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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