react native - can't get url from share intent - android

I want it to be possible to share web pages with my react native app. After the user will share with my app, I need to be able to extract the url.
This is part of my AndroidManifest.xml
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https"/>
<!-- note that the leading "/" is required for pathPrefix-->
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
And this is my code in react native where I am trying to retrieve the url -
useEffect(() => {
Linking.getInitialURL().then((url) => {
if (url) {
alert('got url ', url);
}
}).catch((e) => {
alert('got some error ', e)
});
}, []);
useEffect(() => {
const callback = (url) => alert("get url when app was open " + decodeURI(url));
Linking.addEventListener('url', callback);
return () => {
Linking.removeEventListener('url', callback);
};
}, []);
There are 2 options here - one when the app is already open and the other when the app was opened by the share process.
I don't get the url in any of the options.
I am running the app on a real android devices attached to my laptop in non debug mode.
And it seems like the app keep refreshing itself all the time.
Can someone explain what am I doing wrong?
Thanks

I think you're mixing up deep linking and share menu.
If you want to share urls to your app, you can use this extension https://www.npmjs.com/package/react-native-share-menu. This will add your app as a target while sharing from other apps/browser.
const handleShare = useCallback((item: ?SharedItem) => {
if (!item) {
return;
}
const { mimeType, data, extraData } = item;
setSharedData(data);
setSharedMimeType(mimeType);
// You can receive extra data from your custom Share View
console.log(extraData);
}, []);
useEffect(() => {
ShareMenu.getInitialShare(handleShare);},
[]);
You can find the url youre looking for in "data" field of "item: ?SharedItem"

Related

Flutter application with android fails to capture callback url

I am trying to authenticate a flutter application against Spotify.
For this, I use the flutter_web_auth package. I have a button to authenticate against Spotify.
Once user clicks the button, the following function operates:
onPressed: () async {
final spotifyAuthURL = Uri.https(
'accounts.spotify.com',
'/authorize',
{
'response_type': 'code',
'client_id': dotenv.get('SPOTIFY_CLIENT_ID'),
'redirect_uri':
'https://vinyl_app_spotify.com/callback',
'scope': 'user-read-private user-read-email',
},
);
final result = await FlutterWebAuth.authenticate(
url: spotifyAuthURL.toString(),
callbackUrlScheme:
'https://vinyl_app_spotify.com/callback',
);
},
I've also added the following activiy to the AndroidManifest.xml file as I should have done according to the flutter_web_auth package:
<activity android:name="com.linusu.flutter_web_auth.CallbackActivity" >
<intent-filter android:label="flutter_web_auth">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https://vinyl_app_spotify.com/callback" />
</intent-filter>
</activity>
So after I click my application button, I do get access to Spotify, then the redirect url kicks in, but I just see this:
So my android emulator could not capture the callback url.
What is my mistake? Any advice..

Flutter - oauth2 - Android - redirecturi not working

I have an issue with a flutter implementation on Android. On iOS it works fine.
Here is my AndroidManifest.xml:
<intent-filter android:label="flutter_web_auth">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="com.mycomp.myapp"/>
</intent-filter>
I defined the SpotifyClient like that:
class SpotifyOAuth2Client extends OAuth2Client {
SpotifyOAuth2Client({required String redirectUri, required String customUriScheme}) :
super(
authorizeUrl: 'https://accounts.spotify.com/authorize',
tokenUrl: 'https://accounts.spotify.com/api/token',
redirectUri: redirectUri,
customUriScheme: customUriScheme) {
this.accessTokenRequestHeaders = {'Accept': 'application/json'};
}
}
And here is where I try to call the client:
OAuth2Client client = SpotifyOAuth2Client(
redirectUri: 'com.mycomp.myapp://callback',
customUriScheme: 'com.mycomp.myapp');
var authResp = await client.requestAuthorization(
clientId: 'huhuhuhuhuhuhuhuhu',
customParams: {'show_dialog': 'true'},
scopes: ['user-read-private', 'user-read-playback-state', 'user-modify-playback-state', 'user-read-currently-playing', 'user-read-email']);
print(authResp.code);
The app is correctly triggering the authorization to Spotify. I get to log in and accept on the web browser.
Then I get redirected to myApp but nothing happens. The print(authResp.code) (and of course everything following) above never fires.
Again, on iOS I have no issue.
I have been trying many thing but with no success. Any help would be much appreciated!
After many trials, I have been able to fix the issue.
Hope it can help somebody else.
Adding the following activity (and not only intent-filter in an existing activity) made the trick:
<activity android:name="com.linusu.flutter_web_auth.CallbackActivity" >
<intent-filter android:label="flutter_web_auth">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="com.mycomp.myapp" android:host="callback" />
</intent-filter>
</activity>
hi can you share the code? my code no works when try to add another activity
<activity
android:name="io.flutter.embedding.android.SplashScreenDrawable">
<intent-filter android:label="flutter_web_auth">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="com.mycomp.myapp" android:host="callback" />
</intent-filter>
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="#drawable/launch_background"
/>
</activity>

How to routing in a app when I click a link from email

I want to open my component in Application Android (Nativescript) from a link in email, that is Click here to reset password
I have a link like this in email:
http://secsystem.domain.tk/v1/resetPassword/11E8FC9
So when I click the link from my mobile browser I need the app Launched in component resetPassword.
I use the following code:
AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="secsystem.domain.tk" android:path="/v1/resetPassword"></data>
</intent-filter>
This code works good only in this url: http://secsystem.domain.tk/v1/resetPassword
I want to work in this url: http://secsystem.domain.tk/v1/resetPassword/11E8FC9
In routing.ts I write this code:
{ path: 'v1/resetPassword/:id', component: ResetPassIdComponent },
I want to open this component when I click in link.
Is there any solution for this?
You should use android:pathPattern when the path has dynamic parameter.
Example
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="secsystem.domain.tk" android:path="/v1/resetPassword/.*"></data>
</intent-filter>
So far this is all about making Android understand it has to open this app when this particular URL pattern is matched. Angular has nothing to do here. If you want to navigate to particular component when app is opened with this URL pattern, then you must handle it manually.
Install nativescript-urlhandler plugin and query the URL that was used to open the app in the app component and initiate navigation in your router accordingly.
import { Component, OnInit } from "#angular/core";
import { handleOpenURL, AppURL } from 'nativescript-urlhandler';
#Component({
selector: "gr-main",
template: "<page-router-outlet></page-router-outlet>"
})
export class AppComponent {
constructor() {
}
ngOnInit(){
handleOpenURL((appURL: AppURL) => {
console.log('Got the following appURL', appURL);
});
}
}

React Native OAuth2 authentication with our own Authservice

We have an Authservice which following the OAuth2 protocoll. The url is the following:
https://auth.domaon.com/web/login?client_id=client&login_redirect_uri=%2Fweb%2Fauthorize&redirect_uri=????????&response_type=code&state=somestate
How can I use it to login my users in our mobile app?
I tryed to Linking it.
openAuth() {
Linking.openURL('URL above');
}
componentDidMount() {
Linking.addEventListener('url', this._handleURL);
}
_handleURL(event) {
console.log(event.url);
console.log(event.url.split('#')[1].split('=')[1].split('&')[0]);
}
I don't know what I have to add to my redirect URL to redirect to my app.
I resolved it:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="schema"
android:host="url" />
</intent-filter>
Have to add it to Manifest.xml
App accessible on the schema://url path.

PhoneGap - Call another application (AngryBirds)

I'm implementing phonegap based application for android and want to call angry birds game from inside the phonegap app. After my quick research here and here I have following:
/src/com.borismus.webintent.WebIntent.java
/www/webintent.js
AndroidManifest:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data android:scheme="com.rovio.angrybirds" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
main.js
function onClickStartAngry() {
window.plugins.webintent.startActivity({
action: WebIntent.ACTION_VIEW,
url: 'have_no_idea'},
function() {};
function() {alert('Failed to open AngryBirds App')};
});
}
I'm stuck here and have_no_idea what should be written for url:. Any help? Or there is another way to call for other application from phonegap?
This java code should work
Intent LaunchIntent = this.cordova.getActivity().getPackageManager().getLaunchIntentForPackage("com.rovio.angrybirds");
this.cordova.getActivity().startActivity(LaunchIntent);
or try any of this 2 plugins for opening apps
https://github.com/lampaa/org.apache.cordova.startapp
https://github.com/dmedvinsky/cordova-startapp

Categories

Resources