针对Android 12中关于通知跳板的对应问题,可以通过以下的代码示例进行解决。
在AndroidManifest.xml文件中添加以下代码:
然后在NotificationTrampolineActivity中解析通知跳板链接并处理相关操作,示例如下:
public class NotificationTrampolineActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleNotificationLink(getIntent());
finish();
}
private void handleNotificationLink(Intent intent) {
Uri data = intent.getData();
if (data != null) {
String notificationId = data.getQueryParameter("id");
// 根据通知ID处理相关操作
}
}
}
在发送通知时,可以将通知的ID添加到跳板链接中,如下所示:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("notification://example.com?id=123"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification notification = new Notification.Builder(context)
.setContentTitle("Example Notification")
.setContentText("This is an example notification.")
.setSmallIcon(R.drawable.notification_icon)
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.notify(1, notification);
通过上述代码,可以解决Android 12中关于通知跳板的对应问题。