Laravel 9后端:点击邮箱验证链接唤起Android登录Activity
实现邮箱验证链接唤起Android指定Activity
可以实现,通过Deep Link/App Link结合Laravel后端逻辑调整即可完成,以下是具体步骤:
1. 配置Android端的Intent过滤器
在AndroidManifest.xml中给目标登录Activity添加Intent过滤器,让系统识别特定链接并唤起该页面:
<activity android:name=".LoginActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- 自定义Deep Link Scheme,格式:yourapp://email/verify --> <data android:scheme="yourapp" android:host="email" android:pathPrefix="/verify" /> <!-- 可选:如果用HTTPS域名的App Link,添加以下配置(需域名验证) --> <!-- <data android:scheme="https" android:host="你的域名.com" android:pathPrefix="/email/verify" /> --> </intent-filter> </activity>
2. 替换Laravel默认的邮箱验证通知
默认的sendEmailVerificationNotification()生成网页链接,需自定义通知类生成App可识别的链接:
- 创建自定义验证通知类:
php artisan make:notification CustomEmailVerification
- 在生成的
CustomEmailVerification.php中重写链接生成逻辑:
namespace App\Notifications; use Illuminate\Auth\Notifications\VerifyEmail; use Illuminate\Support\Facades\URL; class CustomEmailVerification extends VerifyEmail { protected function verificationUrl($notifiable) { // 保留Laravel的临时签名验证逻辑 $webVerifyUrl = URL::temporarySignedRoute( 'verification.verify', now()->addMinutes(60), ['id' => $notifiable->getKey(), 'hash' => sha1($notifiable->getEmailForVerification())] ); // 将网页域名替换为自定义Scheme,生成App唤起链接 return str_replace(config('app.url'), 'yourapp://email', $webVerifyUrl); } }
- 在User模型中替换默认通知:
public function sendEmailVerificationNotification() { $this->notify(new \App\Notifications\CustomEmailVerification()); }
3. 调整Laravel验证路由的兼容逻辑
兼顾App唤起和网页验证两种场景,修改routes/web.php中的验证路由:
Route::get('/email/verify/{id}/{hash}', function (Request $request) { $user = \App\Models\User::find($request->route('id')); // 验证hash合法性 if (!hash_equals((string)$request->route('hash'), sha1($user->getEmailForVerification()))) { abort(403); } if ($user->markEmailAsVerified()) { // 判断是否为App唤起请求,返回对应响应 if (str_starts_with($request->header('Referer') ?? '', 'yourapp://')) { return response()->json(['status' => 'success', 'message' => '邮箱验证成功'], 200); } else { // 网页端验证成功提示 return "Your email successfully verified."; } } abort(403); })->middleware(['signed'])->name('verification.verify');
4. Android端处理验证参数
在LoginActivity中获取链接携带的验证参数,调用Laravel接口完成验证:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); handleVerificationIntent(getIntent()); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); handleVerificationIntent(intent); } private void handleVerificationIntent(Intent intent) { Uri data = intent.getData(); if (data != null) { String userId = data.getQueryParameter("id"); String hash = data.getQueryParameter("hash"); // 调用Laravel的验证接口(如https://你的域名.com/email/verify/{userId}/{hash}) // 验证完成后提示用户,并跳转至对应页面 } }
注意事项
- 若使用HTTPS域名的App Link,需在域名根目录放置
assetlinks.json完成官方验证,确保只有你的App能处理该域名链接。 - 需兼容用户未安装App的情况,此时链接会自动跳转到网页验证流程。
内容的提问来源于stack exchange,提问作者max256
相关产品推荐
相关产品推荐

