繁体   English   中英

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

[英]how to open url on click push notification when app is not working in background

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

众所周知,在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);
    }
}

暂无
暂无

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

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