当应用程序无法在后台运行时,如何在点击推送通知中打开网址

时间:2019-01-07 05:49:55

标签: java android push-notification

当我的应用程序不在后台运行时,我想在按下推送通知时打开特定的URL(已杀死)(它将转到该应用程序而不提及网址)。当我的应用正常运行(前景)时,通知正常运行(打开网址)

1 个答案:

答案 0 :(得分:0)

我们知道,在PUSH NOTOFICATION中,任一应用程序是否在后台运行,但是一旦单击PUSH NOTOFICATION,该应用程序就会在其主页上打开。

所以我们要做的是,我们必须通过推送通知发送源,并且必须在启动器活动中将其显示为启动屏幕或主屏幕,并根据此代码。

下面的代码“ AnotherActivity”:“ True”

 { "data": {
    "image": "https://images.pexels.com/photos/87452/flowers-background-butterflies-beautiful-87452.jpeg?w=940&h=650&auto=compress&cs=tinysrgb",
    "message": "Hello Abhishek, There is a offer for you "
    "body": "Grab Upto 60% + Extra 15% OFF on Flowers"
    "AnotherActivity": "True"
  },
  "to" : "/topics/all"
}

此处的活动代码:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FirebaseMessaging.getInstance().subscribeToTopic("all");  
        if (getIntent().getExtras() != null) {

            for (String key : getIntent().getExtras().keySet()) {
                String value = String.valueOf(getIntent().getExtras().getString(key));

                if (key.equals("AnotherActivity") && value.equals("True")) {
                    //Here you can hit Url
                    Intent intent = new Intent(this, AnotherActivity.class);
                    intent.putExtra("value", value);
                    startActivity(intent);
                    finish();
                }

            }
        }

        subscribeToPushService();

    }

    private void subscribeToPushService() {
        FirebaseMessaging.getInstance().subscribeToTopic("news");

        Log.d("AndroidBash", "Subscribed");
        Toast.makeText(MainActivity.this, "Subscribed", Toast.LENGTH_SHORT).show();

        String token = FirebaseInstanceId.getInstance().getToken();

        // Log and toast
        Log.d("AndroidBash", token);
        Toast.makeText(MainActivity.this, token, Toast.LENGTH_SHORT).show();
    }

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