我在我的应用程序中使用 firebase 通知和动态链接
我将深层链接作为通知中的有效负载发送,当用户单击通知时,我将像这样打开深层链接
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(pushAction.getActionDeeplink())); // contains the deeplink
if (pushNotification.getExtraData() != null) { // not extra Data from push
intent.putExtra(IntentKeys.PUSH_DATA_EXTRA, pushNotification.getExtraData());
}
PendingIntent actionPendingIntent =
PendingIntent.getActivity(context, pushNotification.getNotificationId(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(largeIcon)
.setTicker(pushNotification.getTicker())
.setContentTitle(pushNotification.getTitle())
.setContentText(pushNotification.getMessage())
.setAutoCancel(true)
.setContentIntent(actionPendingIntent);
notificationManager.notify(uniqueId, mBuilder.build());
我在我的应用程序中声明了一项活动
<activity
android:name=".ui.activities.SaveActivity"
android:label="@string/title_activity_save"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustPan">
<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:host="example.com"
android:pathPattern="/save"
android:scheme="http" />
</intent-filter>
</activity>
通过通知发送的深层链接 URL 是http://example.com/save
在活动中,我设置了接收这样的深层链接
public class SaveActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save);
setupGoogleApiClient();
}
private void setupGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(AppInvite.API)
.build();
boolean autoLaunchDeepLink = false;
AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, autoLaunchDeepLink)
.setResultCallback(
new ResultCallback<AppInviteInvitationResult>() {
@Override
public void onResult(@NonNull AppInviteInvitationResult result) {
if (result.getStatus().isSuccess()) {
Intent intent = result.getInvitationIntent();
String deepLink = AppInviteReferral.getDeepLink(intent);
Log.d("save getInvitation: found deeplink." + deepLink);
/**************************** ISSUE ***********************/
if(intent.hasExtra(IntentKeys.PUSH_DATA_EXTRA)) {
// there is no extra data :(
}
/**************************** ISSUE ***********************/
} else {
Log.d("save getInvitation: no deep link found.");
}
}
});
}
}
一切都按预期工作。SaveActivity 被调用,它正在打开 SaveActivity。但是,在活动中没有收到额外的数据。
我正在使用 IntentKeys.PUSH_DATA_EXTRA 作为意图中的键设置额外的字符串。